-
Notifications
You must be signed in to change notification settings - Fork 14
/
nb_builder.py
47 lines (26 loc) · 1.15 KB
/
nb_builder.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
import nbformat as nbf
from pathlib import Path
def build_nb(section_dict):
nb = nbf.v4.new_notebook()
# Setup IPYNB file name
nb_name = section_dict['section']
title_string = f"""# {nb_name}
Click [HERE](magic-commands/{nb_name}.ipynb) for a notebook which is runnable in LUSID's JupyterHub using the
`%%luminesce` magic command.
"""
nb['cells'].append(nbf.v4.new_markdown_cell(title_string))
section_files = section_dict["files"]
# Create cells with Luminesce code
for file in section_files:
file_header = f'#### {file[file.find("[") + 1:file.rfind("]")]}'
nb['cells'].append(nbf.v4.new_markdown_cell(file_header))
file_path = Path(file[file.find("(") + 1:file.rfind(")")])
with open(file_path) as f:
sql_code = f.read()
sql_markdown = f"```sql\n{sql_code}\n```"
nb['cells'].append(nbf.v4.new_markdown_cell(sql_markdown))
with open(f'docs/{nb_name}.ipynb', 'w') as f:
nbf.write(nb, f)
def nb_builder(template_data):
for section_dict in template_data:
build_nb(section_dict)