Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nbdev/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 24 additions & 1 deletion nbs/api/11_clean.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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': '<iframe srcdoc=\"...\"></iframe>', 'text/markdown': '```html\\n<p>Hi</p>\\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",
Expand Down
Loading