CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AllenDowney

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: AllenDowney/ModSimPy
Path: blob/master/jb/prep_notebooks.py
Views: 531
1
import nbformat as nbf
2
from glob import glob
3
4
# Collect a list of all notebooks in the content folder
5
notebooks = glob("*.ipynb")
6
7
# Text to look for in adding tags
8
text_search_dict = {
9
"# Solution": "hide-cell", # Hide the cell with a button to show
10
}
11
12
# Search through each notebook and look for the text, add a tag if necessary
13
for ipath in notebooks:
14
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
15
16
for cell in ntbk.cells:
17
cell_tags = cell.get('metadata', {}).get('tags', [])
18
cell_tags = []
19
for key, val in text_search_dict.items():
20
if key in cell['source']:
21
if val not in cell_tags:
22
cell_tags.append(val)
23
if len(cell_tags) > 0:
24
cell['metadata']['tags'] = cell_tags
25
26
nbf.write(ntbk, ipath)
27
28