aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-05-23 14:07:08 +0000
committerEugene Triguba <hello@eugenetriguba.com>2026-05-23 14:07:08 +0000
commit2d5e4794886164249a0723fd6a0d38fee300a7f1 (patch)
tree8bf2a2d715beaf9217307f57b45dd0dffe5d4107
parentAdd env to test (diff)
downloadnb-2d5e4794886164249a0723fd6a0d38fee300a7f1.zip
Remove execution count from outputs when cleaning
-rw-r--r--nb/clean.py12
-rw-r--r--nbs/clean.ipynb73
2 files changed, 78 insertions, 7 deletions
diff --git a/nb/clean.py b/nb/clean.py
index 27a54c1..1d97104 100644
--- a/nb/clean.py
+++ b/nb/clean.py
@@ -6,11 +6,19 @@ from nbformat import NotebookNode
from nb.export import read_nb, PathOrFileLike
+# cell: b436a7c3
+def _clean_cell_output(cell:NotebookNode)->NotebookNode:
+ for o in cell.get('outputs', []):
+ if 'execution_count' in o:
+ o['execution_count'] = None
+ return cell
+
# cell: 4c127179
def _clean_cell(cell:NotebookNode):
if 'execution_count' in cell: cell['execution_count'] = None
- if cell['source'].strip() == '': cell['source'] = []
- return cell
+ if cell['source'] == '': cell['source'] = []
+ cell['metadata'] = {}
+ return _clean_cell_output(cell)
# cell: 944a371b
_allowed_nb_metadata_keys = {"kernelspec", "jupytext", "widgets"}
diff --git a/nbs/clean.ipynb b/nbs/clean.ipynb
index 681a5ce..67519a0 100644
--- a/nbs/clean.ipynb
+++ b/nbs/clean.ipynb
@@ -7,6 +7,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "%%capture\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
@@ -42,6 +43,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "import copy\n",
"from nb import test"
]
},
@@ -56,6 +58,52 @@
]
},
{
+ "cell_type": "markdown",
+ "id": "ecefd668",
+ "metadata": {},
+ "source": [
+ "One of the inherent challenges with Jupyter notebooks is they can have noisy diffs when you use them with version control systems. This can make it harder to tell what has actually changed when reviewing.\n",
+ "\n",
+ "To alleviate this problem, we can 'clean' the notebooks before we check them in."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "4af091bd",
+ "metadata": {},
+ "source": [
+ "If a notebook cell is a code cell, then it will have an 'outputs' section. Some of these output cells have 'execution_count's, which we want to clear because it does not matter how many times a cell was executed when reviewing a change."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b436a7c3",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def _clean_cell_output(cell:NotebookNode)->NotebookNode:\n",
+ " for o in cell.get('outputs', []):\n",
+ " if 'execution_count' in o:\n",
+ " o['execution_count'] = None\n",
+ " return cell"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0823ae66",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "o = nbformat.v4.new_output('execute_result', data={'text/plain': '42'}, execution_count=5)\n",
+ "cc = nbformat.v4.new_code_cell(source=[], outputs=[o])\n",
+ "cleaned = _clean_cell_output(copy.deepcopy(cc))\n",
+ "test.equal(cleaned, {**cc, 'outputs': [{**o, 'execution_count': None}]})"
+ ]
+ },
+ {
"cell_type": "code",
"execution_count": null,
"id": "4c127179",
@@ -65,8 +113,9 @@
"#| export\n",
"def _clean_cell(cell:NotebookNode):\n",
" if 'execution_count' in cell: cell['execution_count'] = None\n",
- " if cell['source'].strip() == '': cell['source'] = []\n",
- " return cell"
+ " if cell['source'] == '': cell['source'] = []\n",
+ " cell['metadata'] = {}\n",
+ " return _clean_cell_output(cell)"
]
},
{
@@ -77,7 +126,8 @@
"outputs": [],
"source": [
"cc = nbformat.v4.new_code_cell(\"print('hello')\", execution_count=10)\n",
- "test.equal(_clean_cell(cc.copy()), {**cc, 'execution_count': None})"
+ "cleaned = _clean_cell(copy.deepcopy(cc))\n",
+ "test.equal(cleaned, {**cc, 'execution_count': None})"
]
},
{
@@ -87,8 +137,21 @@
"metadata": {},
"outputs": [],
"source": [
- "cc = nbformat.v4.new_code_cell(\" \\n \\t\")\n",
- "test.equal(_clean_cell(cc.copy()), {**cc, 'source': []})"
+ "cc = nbformat.v4.new_code_cell(\"\")\n",
+ "cleaned = _clean_cell(copy.deepcopy(cc))\n",
+ "test.equal(cleaned, {**cc, 'source': []})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ef055ecb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "cc = nbformat.v4.new_code_cell(\"print('hello')\", metadata={\"some_key\": 1})\n",
+ "cleaned = _clean_cell(copy.deepcopy(cc))\n",
+ "test.equal(cleaned, {**cc, 'metadata': {}})"
]
},
{