Skip to content

Commit 9f3bce3

Browse files
authored
Merge pull request #1664 from AnswerDotAI/strip-outputs
Support stripping specified output representations
2 parents ee62000 + 1e7979c commit 9f3bce3

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎nbdev/clean.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ def _clean_cell_output_id(lines):
6262

6363
# %% ../nbs/api/11_clean.ipynb #b4cde615
6464
def _clean_cell_output(cell, clean_ids, allowed_out_meta_keys):
65-
"Remove `cell` output execution count and optionally ids from text reprs"
65+
"Remove `cell` output execution count, optionally ids from text reprs, and representations listed under `nbdev.strip`"
6666
outputs = cell.get('outputs', [])
6767
for o in outputs:
6868
if 'execution_count' in o: o['execution_count'] = None
6969
data = o.get('data', {})
7070
data.pop("application/vnd.google.colaboratory.intrinsic+json", None)
71+
for k in nested_idx(o, 'metadata', 'nbdev', 'strip') or []: data.pop(k, None)
7172
for k in data:
7273
if k.startswith('text') and clean_ids: data[k] = _clean_cell_output_id(data[k])
7374
if k.startswith('image') and "svg" not in k: data[k] = data[k].rstrip()

‎nbs/api/11_clean.ipynb‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@
166166
"source": [
167167
"#| export\n",
168168
"def _clean_cell_output(cell, clean_ids, allowed_out_meta_keys):\n",
169-
" \"Remove `cell` output execution count and optionally ids from text reprs\"\n",
169+
" \"Remove `cell` output execution count, optionally ids from text reprs, and representations listed under `nbdev.strip`\"\n",
170170
" outputs = cell.get('outputs', [])\n",
171171
" for o in outputs:\n",
172172
" if 'execution_count' in o: o['execution_count'] = None\n",
173173
" data = o.get('data', {})\n",
174174
" data.pop(\"application/vnd.google.colaboratory.intrinsic+json\", None)\n",
175+
" for k in nested_idx(o, 'metadata', 'nbdev', 'strip') or []: data.pop(k, None)\n",
175176
" for k in data:\n",
176177
" if k.startswith('text') and clean_ids: data[k] = _clean_cell_output_id(data[k])\n",
177178
" if k.startswith('image') and \"svg\" not in k: data[k] = data[k].rstrip()\n",
@@ -379,6 +380,28 @@
379380
"```"
380381
]
381382
},
383+
{
384+
"cell_type": "markdown",
385+
"id": "063fbebb",
386+
"metadata": {},
387+
"source": [
388+
"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:"
389+
]
390+
},
391+
{
392+
"cell_type": "code",
393+
"execution_count": null,
394+
"id": "6148ce9b",
395+
"metadata": {},
396+
"outputs": [],
397+
"source": [
398+
"out = dict(output_type='display_data', metadata={'nbdev': {'strip': ['text/html']}},\n",
399+
" data={'text/html': '<iframe srcdoc=\"...\"></iframe>', 'text/markdown': '```html\\n<p>Hi</p>\\n```'})\n",
400+
"_nb = dict2nb(dict(cells=[mk_cell('preview()', outputs=[out])], metadata={}))\n",
401+
"clean_nb(_nb)\n",
402+
"test_eq(list(_nb.cells[0].outputs[0].data), ['text/markdown'])"
403+
]
404+
},
382405
{
383406
"cell_type": "markdown",
384407
"id": "89d0a249",

0 commit comments

Comments
 (0)