aboutsummaryrefslogtreecommitdiffstats
path: root/nb/clean.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--nb/clean.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/nb/clean.py b/nb/clean.py
index b733591..27a54c1 100644
--- a/nb/clean.py
+++ b/nb/clean.py
@@ -4,16 +4,23 @@
import nbformat
from nbformat import NotebookNode
+from nb.export import read_nb, PathOrFileLike
+
# cell: 4c127179
-def _clean_cell(cell:dict):
+def _clean_cell(cell:NotebookNode):
if 'execution_count' in cell: cell['execution_count'] = None
if cell['source'].strip() == '': cell['source'] = []
return cell
+# cell: 944a371b
+_allowed_nb_metadata_keys = {"kernelspec", "jupytext", "widgets"}
+
# cell: 00c3e419
-def clean_nb(nb:NotebookNode):
+def clean_nb(nb:PathOrFileLike|NotebookNode)->NotebookNode:
"Clean noisy metadata in the notebook."
+ if not isinstance(nb, NotebookNode): nb = read_nb(nb)
for c in nb['cells']: _clean_cell(c)
nb['metadata'] = {k:v for k,v in nb['metadata'].items() if k in _allowed_nb_metadata_keys}
if nb['metadata'].get('kernelspec', {}).get('name'):
nb['metadata']['kernelspec']['display_name'] = nb["metadata"]["kernelspec"]["name"]
+ return nb