-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipynb_to_jekyll.py
117 lines (88 loc) · 3.49 KB
/
ipynb_to_jekyll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
# coding=utf-8
"""
Borrowed and updated from
https://adamj.eu/tech/2014/09/21/using-ipython-notebook-to-write-jekyll-blog-posts/
"""
from __future__ import print_function
import functools
import json
import os
import re
import sys
import io
import glob
def main():
for filename in sys.argv[1:] or ["_posts/*"]:
for one_filename in glob.glob(filename):
if one_filename.endswith(".ipynb"):
convert(one_filename)
def convert(filename):
notebook = json.load(open(filename))
dirname = os.path.dirname(filename)
title = os.path.splitext(os.path.basename(filename))[0]
out_filename = os.path.join(
dirname,
"{}.md".format(title)
)
out_content = ""
mem_file = io.StringIO()
write = functools.partial(print, file=mem_file)
cells = notebook['cells']
for cell in cells:
if cell.get("metadata", {}).get("invisible", False):
continue
try:
if cell['cell_type'] == 'markdown':
# Easy
write(''.join(cell['source']))
elif cell['cell_type'] == 'code':
write("{% capture content %}{% highlight python %}")
write(''.join(cell['source']))
write("{% endhighlight %}{% endcapture %}")
write("{{% include notebook-cell.html execution_count='{}' content=content type='input' %}}".format(
"In: [{}]".format(cell['execution_count'] or " "),
))
unknown_types = {o['output_type'] for o in cell['outputs']} - {'stream', 'execute_result', 'error'}
if unknown_types:
raise ValueError("Unknown types : {}".format(", ".join(unknown_types)))
for output in cell['outputs']:
output_type_css = {
'execute_result': 'output',
'stream': 'print',
'error': 'exception',
}
header = ""
if output['output_type'] == 'execute_result':
content = ''.join(output['data']["text/plain"])
header = "Out: [{}]".format(cell['execution_count'])
else:
if output['output_type'] == 'stream':
content = ''.join(output['text']).strip(" \n")
elif output['output_type'] == 'error':
content = '\n'.join(
strip_colors(o)
for o in output['traceback']
).strip(" \n")
write("{% capture content %}{% highlight pytb %}")
write(content)
write("{% endhighlight %}{% endcapture %}")
write(
"{{% include notebook-cell.html execution_count='{}' "
"content=content type='{}' %}}".format(
header,
output_type_css[output['output_type']]
)
)
except:
print(cell, type(cell))
raise
write("")
with open(out_filename, "w") as out_file:
out_file.write(mem_file.getvalue())
print("{} created.".format(out_filename))
ansi_escape = re.compile(r'\x1b[^m]*m')
def strip_colors(string):
return ansi_escape.sub('', string)
if __name__ == '__main__':
main()