-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_tutorials.py
More file actions
executable file
·53 lines (44 loc) · 1.73 KB
/
convert_tutorials.py
File metadata and controls
executable file
·53 lines (44 loc) · 1.73 KB
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
#!/usr/bin/env python
# coding=utf-8
import os
import argparse
import subprocess
import json
import nbformat
"""
Iterate over readdy_documentation/_tutorials/*.ipynb,
Convert them to basic html,
Read metadata from ipynb and prepend to the html as yaml front matter.
Metadata of the notebook should look like:
{
"readdy" : {
"title": "Internal API",
"position": "1"
},
...
}
"""
__license__ = "LGPL"
__author__ = "chrisfroe"
parser = argparse.ArgumentParser(description='Convert ipython notebooks to readdy_documentation - compatible html')
parser.add_argument('notebook_dir', type=str, help='path to directory where .ipynb files are located')
if __name__ == "__main__":
args = parser.parse_args()
notebook_dir = args.notebook_dir
os.chdir(notebook_dir)
for filename in os.listdir(os.getcwd()):
if filename.endswith(".ipynb"):
print("Converting", filename)
with open(filename, "r") as content_file:
json_string = content_file.read()
notebook = nbformat.reads(json_string, as_version=4)
subprocess.run("jupyter-nbconvert --to html --template basic " + filename, shell=True)
title = notebook.metadata.readdy.title
position = notebook.metadata.readdy.position
front_matter = "---\ntitle: " + title + "\nposition: " + position + "\n---\n"
print("prepending the following front matter\n", front_matter)
filename_as_html = os.path.splitext(filename)[0] + ".html"
with open(filename_as_html, "r") as content_file:
origin = content_file.read()
with open(filename_as_html, "w") as content_file:
content_file.write(front_matter + origin)