forked from amcdawes/QMlabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrub_sols.py
executable file
·43 lines (38 loc) · 1.27 KB
/
scrub_sols.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
#!/usr/bin/env python
"""
simple example script for scrubping solution code cells from IPython notebooks
Usage: `scrub_code.py foo.ipynb [bar.ipynb [...]]`
Marked code cells are scrubbed from the notebook
"""
import io
import os
import sys
from nbformat import read, write, NO_CONVERT
def scrub_code_cells(nb):
scrubbed = 0
cells = 0
#for ws in nb.worksheets:
# for cell in ws.cells:
for cell in nb.cells:
if cell.cell_type != 'code':
continue
cells += 1
# scrub cells marked with initial '# Solution' comment
# any other marker will do, or it could be unconditional
if cell.source.startswith("# Solution"):
cell.source = u'# Solution goes here'
scrubbed += 1
cell.outputs = []
print
print("scrubbed %i/%i code cells from notebook" % (scrubbed, cells))
if __name__ == '__main__':
for ipynb in sys.argv[1:]:
print("scrubbing %s" % ipynb)
with io.open(ipynb, encoding='utf8') as f:
nb = read(f, NO_CONVERT)
scrub_code_cells(nb)
base, ext = os.path.splitext(ipynb)
new_ipynb = "%s_blank%s" % (base, ext)
with io.open(new_ipynb, 'w', encoding='utf8') as f:
write(nb, f)
print("wrote %s" % new_ipynb)