From 1e7979ce57ebe2458b132472d91eb252580d781f Mon Sep 17 00:00:00 2001 From: Jack Hogan Date: Thu, 5 Nov 2026 14:00:57 +0000 Subject: [PATCH] Support stripping specified output representations via nbdev.strip metadata --- nbdev/clean.py | 3 ++- nbs/api/11_clean.ipynb | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/nbdev/clean.py b/nbdev/clean.py index f7243f920..201c2a6d2 100644 --- a/nbdev/clean.py +++ b/nbdev/clean.py @@ -62,12 +62,13 @@ def _clean_cell_output_id(lines): # %% ../nbs/api/11_clean.ipynb #b4cde615 def _clean_cell_output(cell, clean_ids, allowed_out_meta_keys): - "Remove `cell` output execution count and optionally ids from text reprs" + "Remove `cell` output execution count, optionally ids from text reprs, and representations listed under `nbdev.strip`" outputs = cell.get('outputs', []) for o in outputs: if 'execution_count' in o: o['execution_count'] = None data = o.get('data', {}) data.pop("application/vnd.google.colaboratory.intrinsic+json", None) + for k in nested_idx(o, 'metadata', 'nbdev', 'strip') or []: data.pop(k, None) for k in data: if k.startswith('text') and clean_ids: data[k] = _clean_cell_output_id(data[k]) if k.startswith('image') and "svg" not in k: data[k] = data[k].rstrip() diff --git a/nbs/api/11_clean.ipynb b/nbs/api/11_clean.ipynb index 2e487948a..269abd886 100644 --- a/nbs/api/11_clean.ipynb +++ b/nbs/api/11_clean.ipynb @@ -166,12 +166,13 @@ "source": [ "#| export\n", "def _clean_cell_output(cell, clean_ids, allowed_out_meta_keys):\n", - " \"Remove `cell` output execution count and optionally ids from text reprs\"\n", + " \"Remove `cell` output execution count, optionally ids from text reprs, and representations listed under `nbdev.strip`\"\n", " outputs = cell.get('outputs', [])\n", " for o in outputs:\n", " if 'execution_count' in o: o['execution_count'] = None\n", " data = o.get('data', {})\n", " data.pop(\"application/vnd.google.colaboratory.intrinsic+json\", None)\n", + " for k in nested_idx(o, 'metadata', 'nbdev', 'strip') or []: data.pop(k, None)\n", " for k in data:\n", " if k.startswith('text') and clean_ids: data[k] = _clean_cell_output_id(data[k])\n", " if k.startswith('image') and \"svg\" not in k: data[k] = data[k].rstrip()\n", @@ -379,6 +380,28 @@ "```" ] }, + { + "cell_type": "markdown", + "id": "063fbebb", + "metadata": {}, + "source": [ + "A display object with a large, regenerable representation beside a small one can ask for the large one to be removed on clean. It lists the mimetypes to drop under `nbdev.strip` in the output's metadata, which IPython takes from the second element of a `_repr_mimebundle_` return value. `clean_nb` removes those representations and keeps the others:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6148ce9b", + "metadata": {}, + "outputs": [], + "source": [ + "out = dict(output_type='display_data', metadata={'nbdev': {'strip': ['text/html']}},\n", + " data={'text/html': '', 'text/markdown': '```html\\n

Hi

\\n```'})\n", + "_nb = dict2nb(dict(cells=[mk_cell('preview()', outputs=[out])], metadata={}))\n", + "clean_nb(_nb)\n", + "test_eq(list(_nb.cells[0].outputs[0].data), ['text/markdown'])" + ] + }, { "cell_type": "markdown", "id": "89d0a249",