-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_notebook_from_template.py
48 lines (42 loc) · 1.37 KB
/
generate_notebook_from_template.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
import nbformat as nbf
import re
import json
from tqdm import tqdm
VARIABLE_MAP = {
"{{BOSSDB_URI}}": "uri",
"{{X_RANGE}}" : "xs",
"{{Y_RANGE}}" : "ys",
"{{Z_RANGE}}": "zs"
}
# Load the template notebook
with open("template/intern_demo.ipynb", "r") as f:
template_nb = nbf.read(f, as_version=4)
# Get locations
with open('projects.json') as f:
projects = json.load(f)
locations = []
for key, attr in projects.items():
try:
locations.append( (key, attr['locations'][0]))
except KeyError as e:
continue
# Function to replace the undefined variable with a specific value
def replace_variable(cell, values):
if cell['cell_type'] == 'code':
for vname, vkey in VARIABLE_MAP.items():
# Convert ranges to slices
if "RANGE" in vname:
val = f"{values[vkey][0]}:{values[vkey][1]}"
else:
val = values[vkey]
cell['source'] = re.sub(vname, val, cell['source'])
return cell
# Create new notebooks with the specific variable values
for key, location in tqdm(locations):
new_nb = nbf.v4.new_notebook()
for cell in template_nb['cells']:
new_cell = replace_variable(cell.copy(), location)
new_nb['cells'].append(new_cell)
# Save the new notebook
with open(f"notebooks/notebook_{key}.ipynb", "w") as f:
nbf.write(new_nb, f)