forked from AllenDowney/ThinkStats2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_soln.py
More file actions
33 lines (25 loc) · 849 Bytes
/
remove_soln.py
File metadata and controls
33 lines (25 loc) · 849 Bytes
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
import nbformat as nbf
from glob import glob
# Collect a list of all notebooks in the content folder
filenames = glob('chap*soln.ipynb')
text = '# Solution'
replacement = ''
# Search through each notebook
for filename in filenames:
ntbk = nbf.read(filename, nbf.NO_CONVERT)
for cell in ntbk.cells:
# remove tags
if 'tags' in cell['metadata']:
tags = cell['metadata']['tags']
cell['metadata']['tags'] = []
# Remove code from fill-in cells
if 'fill-in' in tags:
cell['source'] = ''
# remove output
if 'outputs' in cell:
cell['outputs'] = []
# remove solutions
if cell['source'].startswith(text):
cell['source'] = replacement
outname = filename.replace('soln', 'ex')
nbf.write(ntbk, outname)