Skip to content

Commit afbd402

Browse files
authored
Revert "Remove widget state"
1 parent 671dc56 commit afbd402

File tree

7 files changed

+31
-111
lines changed

7 files changed

+31
-111
lines changed

TODO.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
- `show_doc` changes:
44
- support `#default_cls_lvl`
55
- pick header level for functions/methods
6+
- implement stuff from nbdev export2html, e.g.(?):
7+
- `remove_widget_state`
68
- fastcore conversion scripts + instuctions
79

nbprocess/_modidx.py

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@
144144
'nbprocess.processors.is_frontmatter': 'https://nbprocess.fast.ai/processors#is_frontmatter',
145145
'nbprocess.processors.lang_identify': 'https://nbprocess.fast.ai/processors#lang_identify',
146146
'nbprocess.processors.nb_fmdict': 'https://nbprocess.fast.ai/processors#nb_fmdict',
147-
'nbprocess.processors.remove_widget_state': 'https://nbprocess.fast.ai/processors#remove_widget_state',
148147
'nbprocess.processors.rm_export': 'https://nbprocess.fast.ai/processors#rm_export',
149148
'nbprocess.processors.rm_header_dash': 'https://nbprocess.fast.ai/processors#rm_header_dash',
150149
'nbprocess.processors.strip_ansi': 'https://nbprocess.fast.ai/processors#strip_ansi'},

nbprocess/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _nothing(self): return []
7777
def base_preprocs(self): return [infer_frontmatter, add_show_docs, insert_warning]
7878
def base_postprocs(self): return []
7979
def base_procs(self):
80-
return [remove_widget_state, strip_ansi, hide_line, filter_stream_, lang_identify, rm_header_dash,
80+
return [strip_ansi, hide_line, filter_stream_, lang_identify, rm_header_dash,
8181
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links]
8282

8383
def procs(self):

nbprocess/processors.py

+12-19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# %% auto 0
44
__all__ = ['DEFAULT_FM_KEYS', 'add_links', 'strip_ansi', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics', 'lang_identify',
5-
'rm_header_dash', 'rm_export', 'clean_show_doc', 'remove_widget_state', 'exec_show_docs', 'insert_warning',
6-
'add_show_docs', 'is_frontmatter', 'nb_fmdict', 'construct_fm', 'insert_frontmatter', 'infer_frontmatter']
5+
'rm_header_dash', 'rm_export', 'clean_show_doc', 'exec_show_docs', 'insert_warning', 'add_show_docs',
6+
'is_frontmatter', 'nb_fmdict', 'construct_fm', 'insert_frontmatter', 'infer_frontmatter']
77

88
# %% ../nbs/09_processors.ipynb 2
99
import ast
@@ -104,13 +104,6 @@ def clean_show_doc(cell):
104104
cell.source = '#| echo: false\n' + cell.source
105105

106106
# %% ../nbs/09_processors.ipynb 30
107-
def remove_widget_state(cell):
108-
"Remove widgets in the output of `cells`"
109-
if cell['cell_type'] == 'code' and 'outputs' in cell:
110-
cell['outputs'] = [l for l in cell['outputs']
111-
if not ('data' in l and 'application/vnd.jupyter.widget-view+json' in l.data)]
112-
113-
# %% ../nbs/09_processors.ipynb 32
114107
_imps = {ast.Import, ast.ImportFrom}
115108

116109
def _show_docs(trees):
@@ -123,7 +116,7 @@ def _do_eval(cell):
123116
if cell.directives_.keys() & _exp_dirs or filter_ex(trees, risinstance(_imps)): return True
124117
if _show_docs(trees): return True
125118

126-
# %% ../nbs/09_processors.ipynb 33
119+
# %% ../nbs/09_processors.ipynb 31
127120
class exec_show_docs:
128121
"Execute cells needed for `show_docs` output, including exported cells and imports"
129122
def __init__(self, nb):
@@ -134,13 +127,13 @@ def __call__(self, cell):
134127
if _do_eval(cell): self.k.cell(cell)
135128
if self.k.exc: raise self.k.exc[1] from None
136129

137-
# %% ../nbs/09_processors.ipynb 36
130+
# %% ../nbs/09_processors.ipynb 34
138131
def insert_warning(nb):
139132
"Insert Autogenerated Warning Into Notebook after the first cell."
140133
content = "<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->"
141134
nb.cells.insert(1, mk_cell(content, False))
142135

143-
# %% ../nbs/09_processors.ipynb 40
136+
# %% ../nbs/09_processors.ipynb 38
144137
_def_types = (ast.FunctionDef,ast.AsyncFunctionDef,ast.ClassDef)
145138
def _def_names(cell, shown):
146139
return [showdoc_nm(o) for o in concat(cell.parsed_()) if isinstance(o,_def_types) and o.name not in shown and o.name[0]!='_']
@@ -151,7 +144,7 @@ def _get_nm(tree):
151144
i = tree.value.args[0]
152145
return f'{i.value.id}.{i.attr}' if isinstance(i, ast.Attribute) else i.id
153146

154-
# %% ../nbs/09_processors.ipynb 41
147+
# %% ../nbs/09_processors.ipynb 39
155148
def add_show_docs(nb):
156149
"Add show_doc cells after exported cells, unless they are already documented"
157150
exports = L(cell for cell in nb.cells if cell.source and _re_exps(cell.source))
@@ -162,7 +155,7 @@ def add_show_docs(nb):
162155
code = f'show_doc({nm})'
163156
nb.cells.insert(cell.idx_+1, mk_cell(code))
164157

165-
# %% ../nbs/09_processors.ipynb 45
158+
# %% ../nbs/09_processors.ipynb 43
166159
_re_title = re.compile(r'^#\s+(.*)[\n\r]+(?:^>\s+(.*))?', flags=re.MULTILINE)
167160
_re_fm = re.compile(r'^---.*\S+.*---', flags=re.DOTALL)
168161
_re_defaultexp = re.compile(r'^\s*#\|\s*default_exp\s+(\S+)', flags=re.MULTILINE)
@@ -173,14 +166,14 @@ def _istitle(cell):
173166
txt = cell.get('source', '')
174167
return bool(_re_title.search(txt)) if txt else False
175168

176-
# %% ../nbs/09_processors.ipynb 46
169+
# %% ../nbs/09_processors.ipynb 44
177170
def _default_exp(nb):
178171
"get the default_exp from a notebook"
179172
code_src = nb.cells.filter(lambda x: x.cell_type == 'code').attrgot('source')
180173
default_exp = first(code_src.filter().map(_re_defaultexp.search).filter())
181174
return default_exp.group(1) if default_exp else None
182175

183-
# %% ../nbs/09_processors.ipynb 48
176+
# %% ../nbs/09_processors.ipynb 46
184177
def nb_fmdict(nb, remove=True):
185178
"Infer the front matter from a notebook's markdown formatting"
186179
md_cells = _celltyp(nb, 'markdown').filter(_istitle)
@@ -196,21 +189,21 @@ def nb_fmdict(nb, remove=True):
196189
return flags
197190
else: return {}
198191

199-
# %% ../nbs/09_processors.ipynb 51
192+
# %% ../nbs/09_processors.ipynb 49
200193
DEFAULT_FM_KEYS = ['title', 'description', 'author', 'image', 'categories', 'output-file', 'aliases']
201194

202195
def construct_fm(fmdict:dict, keys = DEFAULT_FM_KEYS):
203196
"construct front matter from a dictionary, but only for `keys`"
204197
if not fmdict: return None
205198
return '---\n'+'\n'.join([f"{k}: {fmdict[k]}" for k in keys if k in fmdict])+'\n---'
206199

207-
# %% ../nbs/09_processors.ipynb 53
200+
# %% ../nbs/09_processors.ipynb 51
208201
def insert_frontmatter(nb, fm_dict:dict, filter_keys:list=DEFAULT_FM_KEYS):
209202
"Add frontmatter into notebook based on `filter_keys` that exist in `fmdict`."
210203
fm = construct_fm(fm_dict, keys=filter_keys)
211204
if fm: nb.cells.insert(0, NbCell(0, dict(cell_type='raw', metadata={}, source=fm)))
212205

213-
# %% ../nbs/09_processors.ipynb 54
206+
# %% ../nbs/09_processors.ipynb 52
214207
def infer_frontmatter(nb):
215208
"Insert front matter if it doesn't exist automatically from nbdev styled markdown."
216209
if is_frontmatter(nb): return

nbs/09_processors.ipynb

+15-30
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
"metadata": {},
8787
"outputs": [],
8888
"source": [
89-
"def _run_procs(procs=None, preprocs=None, postprocs=None, file='../tests/docs_test.ipynb'):\n",
90-
" nbp = NBProcessor(file, procs, preprocs=preprocs, postprocs=postprocs)\n",
89+
"def _run_procs(procs=None, preprocs=None, postprocs=None):\n",
90+
" nbp = NBProcessor(_test_file, procs, preprocs=preprocs, postprocs=postprocs)\n",
9191
" nbp.process()\n",
9292
" return '\\n'.join([str(cell) for cell in nbp.nb.cells])"
9393
]
@@ -386,33 +386,6 @@
386386
" cell.source = '#| echo: false\\n' + cell.source"
387387
]
388388
},
389-
{
390-
"cell_type": "code",
391-
"execution_count": null,
392-
"id": "91b4d0fc-3586-4cd3-86f6-da39c5f88ec4",
393-
"metadata": {},
394-
"outputs": [],
395-
"source": [
396-
"#|export\n",
397-
"def remove_widget_state(cell):\n",
398-
" \"Remove widgets in the output of `cells`\"\n",
399-
" if cell['cell_type'] == 'code' and 'outputs' in cell:\n",
400-
" cell['outputs'] = [l for l in cell['outputs']\n",
401-
" if not ('data' in l and 'application/vnd.jupyter.widget-view+json' in l.data)]"
402-
]
403-
},
404-
{
405-
"cell_type": "code",
406-
"execution_count": null,
407-
"id": "d91bba21-1beb-45bf-80ff-da4ab3f7fdb8",
408-
"metadata": {},
409-
"outputs": [],
410-
"source": [
411-
"#|hide\n",
412-
"res = _run_procs(remove_widget_state, file='../tests/widget.ipynb')\n",
413-
"assert 'application/vnd.jupyter.widget-view+json' not in res"
414-
]
415-
},
416389
{
417390
"cell_type": "code",
418391
"execution_count": null,
@@ -703,7 +676,19 @@
703676
"execution_count": null,
704677
"id": "edf1ca1d-09a5-4adb-bc28-267a8227bd9b",
705678
"metadata": {},
706-
"outputs": [],
679+
"outputs": [
680+
{
681+
"name": "stdout",
682+
"output_type": "stream",
683+
"text": [
684+
"---\n",
685+
"title: a title\n",
686+
"description: A description\n",
687+
"categories: [c1, c2]\n",
688+
"---\n"
689+
]
690+
}
691+
],
707692
"source": [
708693
"_testdict = nb_fmdict(read_nb('../tests/docs_test.ipynb'))\n",
709694
"_res = construct_fm(_testdict)\n",

nbs/10_cli.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
" def base_preprocs(self): return [infer_frontmatter, add_show_docs, insert_warning]\n",
169169
" def base_postprocs(self): return []\n",
170170
" def base_procs(self):\n",
171-
" return [remove_widget_state, strip_ansi, hide_line, filter_stream_, lang_identify, rm_header_dash, \n",
171+
" return [strip_ansi, hide_line, filter_stream_, lang_identify, rm_header_dash,\n",
172172
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links]\n",
173173
"\n",
174174
" def procs(self):\n",

tests/widget.ipynb

-59
This file was deleted.

0 commit comments

Comments
 (0)