-
Notifications
You must be signed in to change notification settings - Fork 602
/
housekeeping.py
84 lines (76 loc) · 3.07 KB
/
housekeeping.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
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
"""Utility script to be used to cleanup the notebooks before git commit"""
import shutil
import sys
import os
import io
from IPython.nbformat import current
def remove_outputs(nb):
"""Remove the outputs from a notebook"""
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
cell.outputs = []
if 'prompt_number' in cell:
del cell['prompt_number']
def remove_solutions(nb):
"""Generate a version of the notebook with stripped exercises solutions"""
for ws in nb.worksheets:
inside_solution = False
cells_to_remove = []
for i, cell in enumerate(ws.cells):
if cell.cell_type == 'heading':
inside_solution = False
elif cell.cell_type == 'markdown':
first_line = cell.source.split("\n")[0].strip()
if first_line.lower() in ("**exercise:**", "**exercise**:"):
inside_solution = True
# Insert a new code cell to work on the exercise
ws.cells.insert(i + 1, current.new_code_cell())
continue
if inside_solution:
if cell.cell_type == 'code' and not hasattr(cell, 'input'):
# Leave blank code cells
continue
cells_to_remove.append(cell)
for cell in cells_to_remove:
ws.cells.remove(cell)
if __name__ == '__main__':
cmd = sys.argv[1]
if cmd == 'clean':
target = sys.argv[2]
if os.path.isdir(target):
fnames = [os.path.join(target, f)
for f in os.listdir(target)
if f.endswith('.ipynb')]
else:
fnames = [target]
for fname in fnames:
print("Removing outputs for: " + fname)
with open(fname, 'r') as f:
nb = current.read(f, 'json')
remove_outputs(nb)
with open(fname, 'w') as f:
nb = current.write(nb, f, 'json')
elif cmd == 'exercises':
# Copy the images from the solutions to the notebooks folder
solutions_images = os.path.join('solutions', 'images')
notebooks_images = os.path.join('notebooks', 'images')
if os.path.exists(notebooks_images):
shutil.rmtree(notebooks_images)
shutil.copytree(solutions_images, notebooks_images)
# Generate the notebooks without the exercises solutions
fnames = [f for f in os.listdir('solutions')
if f.endswith('.ipynb')]
for fname in fnames:
solution = os.path.join('solutions', fname)
notebook = os.path.join('notebooks', fname)
print("Generating solution-free notebook: " + notebook)
with open(solution, 'r') as f:
nb = current.read(f, 'json')
remove_solutions(nb)
remove_outputs(nb)
with open(notebook, 'w') as f:
nb = current.write(nb, f, 'json')
else:
print("Unsupported command")
sys.exit(1)