Skip to content

Commit

Permalink
Merge pull request #1113 from seeM/store-widget-state
Browse files Browse the repository at this point in the history
`exec_doc` supports re-rendering widgets
  • Loading branch information
hamelsmu authored Sep 23, 2022
2 parents 17bbb0a + 13950e1 commit aa9c1d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
'nbdev/processors.py'),
'nbdev.processors.exec_show_docs.begin': ( 'api/processors.html#exec_show_docs.begin',
'nbdev/processors.py'),
'nbdev.processors.exec_show_docs.end': ('api/processors.html#exec_show_docs.end', 'nbdev/processors.py'),
'nbdev.processors.filter_stream_': ('api/processors.html#filter_stream_', 'nbdev/processors.py'),
'nbdev.processors.hide_': ('api/processors.html#hide_', 'nbdev/processors.py'),
'nbdev.processors.hide_line': ('api/processors.html#hide_line', 'nbdev/processors.py'),
Expand Down
10 changes: 10 additions & 0 deletions nbdev/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ def __call__(self, cell):
if self.k.exc:
raise Exception(f"Error{' in notebook: '+title if title else ''} in cell {cell.idx_} :\n{cell.source}") from self.k.exc[1]

def end(self):
try: from ipywidgets import Widget
except ImportError: pass
else:
mimetype = 'application/vnd.jupyter.widget-state+json'
old = nested_idx(self.nb.metadata, 'widgets', mimetype) or {'state': {}}
new = Widget.get_manager_state(drop_defaults=True)
widgets = {**old, **new, 'state': {**old['state'], **new['state']}}
self.nb.metadata['widgets'] = {mimetype: widgets}

# %% ../nbs/api/processors.ipynb 42
class FilterDefaults:
"Override `FilterDefaults` to change which notebook processors are used"
Expand Down
12 changes: 11 additions & 1 deletion nbs/api/processors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,17 @@
" if _do_eval(cell): self.k.cell(cell)\n",
" title = fm.get('title', '')\n",
" if self.k.exc: \n",
" raise Exception(f\"Error{' in notebook: '+title if title else ''} in cell {cell.idx_} :\\n{cell.source}\") from self.k.exc[1]"
" raise Exception(f\"Error{' in notebook: '+title if title else ''} in cell {cell.idx_} :\\n{cell.source}\") from self.k.exc[1]\n",
"\n",
" def end(self):\n",
" try: from ipywidgets import Widget\n",
" except ImportError: pass\n",
" else:\n",
" mimetype = 'application/vnd.jupyter.widget-state+json'\n",
" old = nested_idx(self.nb.metadata, 'widgets', mimetype) or {'state': {}}\n",
" new = Widget.get_manager_state(drop_defaults=True)\n",
" widgets = {**old, **new, 'state': {**old['state'], **new['state']}}\n",
" self.nb.metadata['widgets'] = {mimetype: widgets}"
]
},
{
Expand Down

0 comments on commit aa9c1d7

Please sign in to comment.