-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_scripts.py
More file actions
47 lines (38 loc) · 1.57 KB
/
generate_scripts.py
File metadata and controls
47 lines (38 loc) · 1.57 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
import os
import glob
import json, re
from pathlib import Path
import io
def is_export(cell):
if cell['cell_type'] != 'code':
return False
src = cell['source']
if len(src) == 0 or len(src[0]) < 7:
return False
return re.match(r'^\s*#\s*export\s*$', src[0], re.IGNORECASE) is not None
def notebook2script(f_name):
"Finds cells starting with `#export` and puts them into a new module"
f_name = Path(f_name)
main_dic = json.load(open(f_name,'r',encoding="utf-8"))
code_cells = [c for c in main_dic['cells'] if is_export(c)]
module = '# ---------------------------------------------\n' + '# | THIS FILE WAS AUTOGENERATED! DO NOT EDIT! |\n' + '# ---------------------------------------------\n' + f'# edit notebooks/{f_name.name} and run generate_all.py\n\n'
if not (Path('scripts')).exists():
('scripts').mkdir()
output_path = 'scripts/' + f'{f_name.stem[3:]}.py'
for cell in code_cells:
module += ''.join(cell['source'][1:]) + '\n\n'
module = re.sub(r' +$', '', module, flags=re.MULTILINE)
with io.open(output_path, "w", encoding="utf-8") as f:
f.write(module[:-2])
print(f"{str(f_name):35} -> {str(output_path)}")
def clean():
files = glob.glob('scripts/*')
for f_name in files:
if f_name.endswith('py'):
os.remove(f_name)
def generate():
for f_name in sorted(glob.glob('notebooks/*')):
if f_name.endswith('ipynb'):
notebook2script(f_name)
clean()
generate()