Skip to content

Commit 4b967c7

Browse files
committed
fix notebooks for rendering on github
1 parent 82c0069 commit 4b967c7

5 files changed

Lines changed: 93 additions & 98 deletions

File tree

notebook1-4_test/notebook1-4_test.ipynb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
"*Developed by Mark Bakker*"
1313
]
1414
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"import numpy as np\n",
22+
"import matplotlib.pyplot as plt"
23+
]
24+
},
1525
{
1626
"cell_type": "markdown",
1727
"metadata": {},
@@ -31,17 +41,6 @@
3141
"Use `plt.axis('scaled')` to make sure your squares look like squares and not rectangles."
3242
]
3343
},
34-
{
35-
"cell_type": "code",
36-
"execution_count": 1,
37-
"metadata": {},
38-
"outputs": [],
39-
"source": [
40-
"%matplotlib inline\n",
41-
"import numpy as np\n",
42-
"import matplotlib.pyplot as plt"
43-
]
44-
},
4544
{
4645
"cell_type": "code",
4746
"execution_count": null,
@@ -205,7 +204,7 @@
205204
"metadata": {
206205
"anaconda-cloud": {},
207206
"kernelspec": {
208-
"display_name": "Python 3",
207+
"display_name": "Python 3 (ipykernel)",
209208
"language": "python",
210209
"name": "python3"
211210
},
@@ -219,9 +218,9 @@
219218
"name": "python",
220219
"nbconvert_exporter": "python",
221220
"pygments_lexer": "ipython3",
222-
"version": "3.7.4"
221+
"version": "3.13.5"
223222
}
224223
},
225224
"nbformat": 4,
226-
"nbformat_minor": 1
225+
"nbformat_minor": 4
227226
}

notebook2_arrays/clean_notebook.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import nbformat
2+
import sys
3+
from pathlib import Path
4+
5+
def clean_notebook_metadata(path: str):
6+
"""
7+
Remove problematic metadata that breaks GitHub rendering,
8+
while keeping all cell outputs intact.
9+
"""
10+
path = Path(path)
11+
if not path.exists():
12+
print(f"File not found: {path}")
13+
return
14+
15+
# Read notebook
16+
nb = nbformat.read(str(path), as_version=4)
17+
18+
# Keys in top-level metadata that trigger GitHub widget errors
19+
for key in ["widgets", "varInspector", "latex_envs"]:
20+
if key in nb['metadata']:
21+
nb['metadata'].pop(key, None)
22+
print(f"Removed top-level metadata: {key}")
23+
24+
# Optional: Remove widgets metadata from individual cells if present
25+
for i, cell in enumerate(nb['cells']):
26+
if 'metadata' in cell and 'widgets' in cell['metadata']:
27+
cell['metadata'].pop('widgets', None)
28+
print(f"Removed widgets metadata from cell {i}")
29+
30+
# Write the cleaned notebook back
31+
nbformat.write(nb, str(path))
32+
print(f"Notebook cleaned and saved: {path}")
33+
34+
if __name__ == "__main__":
35+
if len(sys.argv) < 2:
36+
print("Usage: python clean_notebook.py <notebook.ipynb>")
37+
sys.exit(1)
38+
39+
notebook_path = sys.argv[1]
40+
clean_notebook_metadata(notebook_path)

notebook2_arrays/py_exploratory_comp_2_sol.ipynb

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,57 +1293,6 @@
12931293
"nbconvert_exporter": "python",
12941294
"pygments_lexer": "ipython3",
12951295
"version": "3.13.5"
1296-
},
1297-
"latex_envs": {
1298-
"LaTeX_envs_menu_present": true,
1299-
"autoclose": false,
1300-
"autocomplete": true,
1301-
"bibliofile": "biblio.bib",
1302-
"cite_by": "apalike",
1303-
"current_citInitial": 1,
1304-
"eqLabelWithNumbers": true,
1305-
"eqNumInitial": 1,
1306-
"hotkeys": {
1307-
"equation": "Ctrl-E",
1308-
"itemize": "Ctrl-I"
1309-
},
1310-
"labels_anchors": false,
1311-
"latex_user_defs": false,
1312-
"report_style_numbering": false,
1313-
"user_envs_cfg": false
1314-
},
1315-
"varInspector": {
1316-
"cols": {
1317-
"lenName": 16,
1318-
"lenType": 16,
1319-
"lenVar": 40
1320-
},
1321-
"kernels_config": {
1322-
"python": {
1323-
"delete_cmd_postfix": "",
1324-
"delete_cmd_prefix": "del ",
1325-
"library": "var_list.py",
1326-
"varRefreshCmd": "print(var_dic_list())"
1327-
},
1328-
"r": {
1329-
"delete_cmd_postfix": ") ",
1330-
"delete_cmd_prefix": "rm(",
1331-
"library": "var_list.r",
1332-
"varRefreshCmd": "cat(var_dic_list()) "
1333-
}
1334-
},
1335-
"types_to_exclude": [
1336-
"module",
1337-
"function",
1338-
"builtin_function_or_method",
1339-
"instance",
1340-
"_Feature"
1341-
],
1342-
"window_display": false
1343-
},
1344-
"widgets": {
1345-
"state": {},
1346-
"version": "1.1.2"
13471296
}
13481297
},
13491298
"nbformat": 4,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import nbformat
2+
import sys
3+
from pathlib import Path
4+
5+
def clean_notebook_metadata(path: str):
6+
"""
7+
Remove problematic metadata that breaks GitHub rendering,
8+
while keeping all cell outputs intact.
9+
"""
10+
path = Path(path)
11+
if not path.exists():
12+
print(f"File not found: {path}")
13+
return
14+
15+
# Read notebook
16+
nb = nbformat.read(str(path), as_version=4)
17+
18+
# Keys in top-level metadata that trigger GitHub widget errors
19+
for key in ["widgets", "varInspector", "latex_envs"]:
20+
if key in nb['metadata']:
21+
nb['metadata'].pop(key, None)
22+
print(f"Removed top-level metadata: {key}")
23+
24+
# Optional: Remove widgets metadata from individual cells if present
25+
for i, cell in enumerate(nb['cells']):
26+
if 'metadata' in cell and 'widgets' in cell['metadata']:
27+
cell['metadata'].pop('widgets', None)
28+
print(f"Removed widgets metadata from cell {i}")
29+
30+
# Write the cleaned notebook back
31+
nbformat.write(nb, str(path))
32+
print(f"Notebook cleaned and saved: {path}")
33+
34+
if __name__ == "__main__":
35+
if len(sys.argv) < 2:
36+
print("Usage: python clean_notebook.py <notebook.ipynb>")
37+
sys.exit(1)
38+
39+
notebook_path = sys.argv[1]
40+
clean_notebook_metadata(notebook_path)

notebook4_functions/py_exploratory_comp_4_sol.ipynb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,39 +1264,6 @@
12641264
"nbconvert_exporter": "python",
12651265
"pygments_lexer": "ipython3",
12661266
"version": "3.13.5"
1267-
},
1268-
"varInspector": {
1269-
"cols": {
1270-
"lenName": 16,
1271-
"lenType": 16,
1272-
"lenVar": 40
1273-
},
1274-
"kernels_config": {
1275-
"python": {
1276-
"delete_cmd_postfix": "",
1277-
"delete_cmd_prefix": "del ",
1278-
"library": "var_list.py",
1279-
"varRefreshCmd": "print(var_dic_list())"
1280-
},
1281-
"r": {
1282-
"delete_cmd_postfix": ") ",
1283-
"delete_cmd_prefix": "rm(",
1284-
"library": "var_list.r",
1285-
"varRefreshCmd": "cat(var_dic_list()) "
1286-
}
1287-
},
1288-
"types_to_exclude": [
1289-
"module",
1290-
"function",
1291-
"builtin_function_or_method",
1292-
"instance",
1293-
"_Feature"
1294-
],
1295-
"window_display": false
1296-
},
1297-
"widgets": {
1298-
"state": {},
1299-
"version": "1.1.2"
13001267
}
13011268
},
13021269
"nbformat": 4,

0 commit comments

Comments
 (0)