Skip to content

Commit 929c2d5

Browse files
committed
updated notebook clearing script to be more in-depth. Also, output detector was not working so just run on all
1 parent c147bd1 commit 929c2d5

27 files changed

Lines changed: 67 additions & 1630 deletions

notebooks/clear_all_notebooks.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pathlib as pl
33
import json
4+
import subprocess
45

56
notebook_count = 0
67
# add the basenames of any notebooks we want to keep the results of
@@ -18,30 +19,22 @@
1819
'10_modpath_particle_tracking-demo.ipynb'
1920
]
2021

21-
def check_nb_for_output(notebook):
22-
has_output = False
23-
# run autotest on each notebook
24-
with open(notebook) as src:
25-
data = src.read()
26-
data = json.loads(data)
27-
for item in data['cells']:
28-
for k, v in item.items():
29-
if item['cell_type'] == 'code':
30-
if any (item['outputs']):
31-
has_output = True
32-
return has_output
33-
34-
3522
if __name__ == "__main__":
36-
37-
for [path, subdirs, files] in os.walk('.'):
38-
for cf in files:
39-
if cf.lower().endswith('.ipynb') and '.ipynb_checkpoint' not in path:
40-
nb = pl.Path(path) / cf
41-
if ('solutions' not in str(nb) and
42-
nb.name not in skip_notebooks and
43-
check_nb_for_output(nb) is True):
44-
print(f"clearing {nb}")
45-
os.system(f"jupyter nbconvert --clear-output --inplace {nb._str}")
46-
notebook_count += 1
47-
print(notebook_count," notebooks cleared")
23+
nbdir = pl.Path(".")
24+
nbs = nbdir.rglob("*.ipynb")
25+
for nb in nbs:
26+
if ('solutions' not in str(nb) and
27+
nb.name not in skip_notebooks):
28+
print("clearing", nb)
29+
cmd = (
30+
"jupyter",
31+
"nbconvert",
32+
"--ClearOutputPreprocessor.enabled=True",
33+
"--ClearMetadataPreprocessor.enabled=True",
34+
"--ClearMetadataPreprocessor.preserve_nb_metadata_mask={('kernelspec')}",
35+
"--inplace",
36+
nb,
37+
)
38+
proc = subprocess.run(cmd)
39+
assert proc.returncode == 0, f"Error running command: {' '.join(cmd)}"
40+

notebooks/part0_python_intro/00_python_basics_review.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,18 +1379,6 @@
13791379
"display_name": "Python 3 (ipykernel)",
13801380
"language": "python",
13811381
"name": "python3"
1382-
},
1383-
"language_info": {
1384-
"codemirror_mode": {
1385-
"name": "ipython",
1386-
"version": 3
1387-
},
1388-
"file_extension": ".py",
1389-
"mimetype": "text/x-python",
1390-
"name": "python",
1391-
"nbconvert_exporter": "python",
1392-
"pygments_lexer": "ipython3",
1393-
"version": "3.11.10"
13941382
}
13951383
},
13961384
"nbformat": 4,

notebooks/part0_python_intro/00_skills_test_on_basics.ipynb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@
9191
"cell_type": "code",
9292
"execution_count": null,
9393
"id": "b755a4ce",
94-
"metadata": {
95-
"scrolled": true
96-
},
94+
"metadata": {},
9795
"outputs": [],
9896
"source": [
9997
"# finally split on \"whitespace\" resulting in a list of words\n",
@@ -287,18 +285,6 @@
287285
"display_name": "Python 3 (ipykernel)",
288286
"language": "python",
289287
"name": "python3"
290-
},
291-
"language_info": {
292-
"codemirror_mode": {
293-
"name": "ipython",
294-
"version": 3
295-
},
296-
"file_extension": ".py",
297-
"mimetype": "text/x-python",
298-
"name": "python",
299-
"nbconvert_exporter": "python",
300-
"pygments_lexer": "ipython3",
301-
"version": "3.11.13"
302288
}
303289
},
304290
"nbformat": 4,

notebooks/part0_python_intro/01_functions_scripts.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -694,18 +694,6 @@
694694
"display_name": "pyclass",
695695
"language": "python",
696696
"name": "python3"
697-
},
698-
"language_info": {
699-
"codemirror_mode": {
700-
"name": "ipython",
701-
"version": 3
702-
},
703-
"file_extension": ".py",
704-
"mimetype": "text/x-python",
705-
"name": "python",
706-
"nbconvert_exporter": "python",
707-
"pygments_lexer": "ipython3",
708-
"version": "3.12.11"
709697
}
710698
},
711699
"nbformat": 4,

notebooks/part0_python_intro/02_Namespace_objects_modules_packages.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,18 +1605,6 @@
16051605
"display_name": "Python 3 (ipykernel)",
16061606
"language": "python",
16071607
"name": "python3"
1608-
},
1609-
"language_info": {
1610-
"codemirror_mode": {
1611-
"name": "ipython",
1612-
"version": 3
1613-
},
1614-
"file_extension": ".py",
1615-
"mimetype": "text/x-python",
1616-
"name": "python",
1617-
"nbconvert_exporter": "python",
1618-
"pygments_lexer": "ipython3",
1619-
"version": "3.11.7"
16201608
}
16211609
},
16221610
"nbformat": 4,

notebooks/part0_python_intro/03_useful-std-library-modules.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,18 +1252,6 @@
12521252
"display_name": "pyclass",
12531253
"language": "python",
12541254
"name": "python3"
1255-
},
1256-
"language_info": {
1257-
"codemirror_mode": {
1258-
"name": "ipython",
1259-
"version": 3
1260-
},
1261-
"file_extension": ".py",
1262-
"mimetype": "text/x-python",
1263-
"name": "python",
1264-
"nbconvert_exporter": "python",
1265-
"pygments_lexer": "ipython3",
1266-
"version": "3.11.7"
12671255
}
12681256
},
12691257
"nbformat": 4,

notebooks/part0_python_intro/04_files_and_strings.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -843,18 +843,6 @@
843843
"display_name": "Python 3 (ipykernel)",
844844
"language": "python",
845845
"name": "python3"
846-
},
847-
"language_info": {
848-
"codemirror_mode": {
849-
"name": "ipython",
850-
"version": 3
851-
},
852-
"file_extension": ".py",
853-
"mimetype": "text/x-python",
854-
"name": "python",
855-
"nbconvert_exporter": "python",
856-
"pygments_lexer": "ipython3",
857-
"version": "3.12.1"
858846
}
859847
},
860848
"nbformat": 4,

notebooks/part0_python_intro/05_numpy.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,18 +1760,6 @@
17601760
"display_name": "Python 3 (ipykernel)",
17611761
"language": "python",
17621762
"name": "python3"
1763-
},
1764-
"language_info": {
1765-
"codemirror_mode": {
1766-
"name": "ipython",
1767-
"version": 3
1768-
},
1769-
"file_extension": ".py",
1770-
"mimetype": "text/x-python",
1771-
"name": "python",
1772-
"nbconvert_exporter": "python",
1773-
"pygments_lexer": "ipython3",
1774-
"version": "3.12.11"
17751763
}
17761764
},
17771765
"nbformat": 4,

notebooks/part0_python_intro/06_matplotlib.ipynb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,18 +1180,6 @@
11801180
"display_name": "lisus_p2",
11811181
"language": "python",
11821182
"name": "python3"
1183-
},
1184-
"language_info": {
1185-
"codemirror_mode": {
1186-
"name": "ipython",
1187-
"version": 3
1188-
},
1189-
"file_extension": ".py",
1190-
"mimetype": "text/x-python",
1191-
"name": "python",
1192-
"nbconvert_exporter": "python",
1193-
"pygments_lexer": "ipython3",
1194-
"version": "3.11.10"
11951183
}
11961184
},
11971185
"nbformat": 4,

notebooks/part0_python_intro/06b_matplotlib_animation.ipynb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@
9494
"source": []
9595
}
9696
],
97-
"metadata": {
98-
"language_info": {
99-
"name": "python"
100-
}
101-
},
97+
"metadata": {},
10298
"nbformat": 4,
10399
"nbformat_minor": 2
104100
}

0 commit comments

Comments
 (0)