forked from AtsushiSakai/PythonRobotics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjupyternotebook2rst.py
More file actions
93 lines (64 loc) · 1.8 KB
/
jupyternotebook2rst.py
File metadata and controls
93 lines (64 loc) · 1.8 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
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
"""
Jupyter notebook converter to rst file
author: Atsushi Sakai
"""
import subprocess
import os.path
import os
import glob
NOTEBOOK_DIR = "../"
def get_notebook_path_list(ndir):
path = glob.glob(ndir + "**/*.ipynb", recursive=True)
return path
def convert_rst(rstpath):
with open(rstpath, "r") as bfile:
filedata = bfile.read()
# convert from code directive to code-block
# because showing code in Sphinx
before = ".. code:: ipython3"
after = ".. code-block:: ipython3"
filedata = filedata.replace(before, after)
with open(rstpath, "w") as afile:
afile.write(filedata)
def generate_rst(npath):
print("====Start generating rst======")
# generate dir
dirpath = os.path.dirname(npath)
# print(dirpath)
rstpath = os.path.abspath("./modules/" + npath[3:-5] + "rst")
# print(rstpath)
basename = os.path.basename(rstpath)
cmd = "jupyter nbconvert --to rst "
cmd += npath
print(cmd)
subprocess.call(cmd, shell=True)
rstpath = dirpath + "/" + basename
convert_rst(rstpath)
# clean up old files
cmd = "rm -rf "
cmd += "./modules/"
cmd += basename[:-4]
cmd += "*"
# print(cmd)
subprocess.call(cmd, shell=True)
# move files to module dir
cmd = "mv "
cmd += dirpath
cmd += "/*.rst ./modules/"
print(cmd)
subprocess.call(cmd, shell=True)
cmd = "mv "
cmd += dirpath
cmd += "/*_files ./modules/"
print(cmd)
subprocess.call(cmd, shell=True)
def main():
print("start!!")
notebook_path_list = get_notebook_path_list(NOTEBOOK_DIR)
# print(notebook_path_list)
for npath in notebook_path_list:
if "template" not in npath:
generate_rst(npath)
print("done!!")
if __name__ == '__main__':
main()