Skip to content

Commit 3951d8a

Browse files
committed
fixes #1407
1 parent 949b30f commit 3951d8a

File tree

5 files changed

+64
-22
lines changed

5 files changed

+64
-22
lines changed

nbdev/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
'nbdev.processors._re_hideline': ('api/processors.html#_re_hideline', 'nbdev/processors.py'),
199199
'nbdev.processors._show_docs': ('api/processors.html#_show_docs', 'nbdev/processors.py'),
200200
'nbdev.processors._want_doc': ('api/processors.html#_want_doc', 'nbdev/processors.py'),
201+
'nbdev.processors.add_fold': ('api/processors.html#add_fold', 'nbdev/processors.py'),
201202
'nbdev.processors.add_links': ('api/processors.html#add_links', 'nbdev/processors.py'),
202203
'nbdev.processors.add_show_docs': ('api/processors.html#add_show_docs', 'nbdev/processors.py'),
203204
'nbdev.processors.add_show_docs.begin': ( 'api/processors.html#add_show_docs.begin',

nbdev/processors.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/10_processors.ipynb.
22

33
# %% auto 0
4-
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'add_links', 'strip_ansi',
4+
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'add_links', 'add_fold', 'strip_ansi',
55
'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics', 'rm_header_dash',
66
'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']
77

@@ -75,7 +75,8 @@ def begin(self):
7575
shown_docs = {_get_nm(t) for t in _show_docs(trees)}
7676
for cell in reversed(exports):
7777
if cell_lang(cell) != 'python': raise ValueError(f"{cell.metadata.language} can't export:\n{cell.source}")
78-
for nm in _def_names(cell, shown_docs): nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))
78+
nms = _def_names(cell, shown_docs)
79+
for nm in nms: nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))
7980
nb.has_docs_ = shown_docs or exports
8081

8182
# %% ../nbs/api/10_processors.ipynb 17
@@ -96,25 +97,31 @@ def add_links(cell):
9697
if hasattr(o, 'data') and hasattr(o['data'], 'text/markdown'):
9798
o.data['text/markdown'] = [nl.link_line(s) for s in o.data['text/markdown']]
9899

99-
# %% ../nbs/api/10_processors.ipynb 22
100+
# %% ../nbs/api/10_processors.ipynb 21
101+
def add_fold(cell):
102+
"Add `code-fold` to `exports` cells"
103+
if cell.cell_type != 'code' or 'exports' not in cell.directives_: return
104+
cell.source = f'#| code-fold: show\n#| code-summary: "Exported source"\n{cell.source}'
105+
106+
# %% ../nbs/api/10_processors.ipynb 24
100107
_re_ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
101108

102109
def strip_ansi(cell):
103110
"Strip Ansi Characters."
104111
for outp in cell.get('outputs', []):
105112
if outp.get('name')=='stdout': outp['text'] = [_re_ansi_escape.sub('', o) for o in outp.text]
106113

107-
# %% ../nbs/api/10_processors.ipynb 24
114+
# %% ../nbs/api/10_processors.ipynb 26
108115
def strip_hidden_metadata(cell):
109116
'''Strips "hidden" metadata property from code cells so it doesn't interfere with docs rendering'''
110117
if cell.cell_type == 'code' and 'metadata' in cell: cell.metadata.pop('hidden',None)
111118

112-
# %% ../nbs/api/10_processors.ipynb 25
119+
# %% ../nbs/api/10_processors.ipynb 27
113120
def hide_(cell):
114121
"Hide cell from output"
115122
del(cell['source'])
116123

117-
# %% ../nbs/api/10_processors.ipynb 27
124+
# %% ../nbs/api/10_processors.ipynb 29
118125
def _re_hideline(lang=None): return re.compile(fr'{langs[lang]}\|\s*hide_line\s*$', re.MULTILINE)
119126

120127
def hide_line(cell):
@@ -123,22 +130,22 @@ def hide_line(cell):
123130
if cell.cell_type == 'code' and _re_hideline(lang).search(cell.source):
124131
cell.source = '\n'.join([c for c in cell.source.splitlines() if not _re_hideline(lang).search(c)])
125132

126-
# %% ../nbs/api/10_processors.ipynb 30
133+
# %% ../nbs/api/10_processors.ipynb 32
127134
def filter_stream_(cell, *words):
128135
"Remove output lines containing any of `words` in `cell` stream output"
129136
if not words: return
130137
for outp in cell.get('outputs', []):
131138
if outp.output_type == 'stream':
132139
outp['text'] = [l for l in outp.text if not re.search('|'.join(words), l)]
133140

134-
# %% ../nbs/api/10_processors.ipynb 32
141+
# %% ../nbs/api/10_processors.ipynb 34
135142
_magics_pattern = re.compile(r'^\s*(%%|%).*', re.MULTILINE)
136143

137144
def clean_magics(cell):
138145
"A preprocessor to remove cell magic commands"
139146
if cell.cell_type == 'code': cell.source = _magics_pattern.sub('', cell.source).strip()
140147

141-
# %% ../nbs/api/10_processors.ipynb 34
148+
# %% ../nbs/api/10_processors.ipynb 36
142149
_re_hdr_dash = re.compile(r'^#+\s+.*\s+-\s*$', re.MULTILINE)
143150

144151
def rm_header_dash(cell):
@@ -147,14 +154,14 @@ def rm_header_dash(cell):
147154
src = cell.source.strip()
148155
if cell.cell_type == 'markdown' and src.startswith('#') and src.endswith(' -'): del(cell['source'])
149156

150-
# %% ../nbs/api/10_processors.ipynb 36
157+
# %% ../nbs/api/10_processors.ipynb 38
151158
_hide_dirs = {'export','exporti', 'hide','default_exp'}
152159

153160
def rm_export(cell):
154161
"Remove cells that are exported or hidden"
155162
if cell.directives_ and (cell.directives_.keys() & _hide_dirs): del(cell['source'])
156163

157-
# %% ../nbs/api/10_processors.ipynb 38
164+
# %% ../nbs/api/10_processors.ipynb 40
158165
_re_showdoc = re.compile(r'^show_doc', re.MULTILINE)
159166
def _is_showdoc(cell): return cell['cell_type'] == 'code' and _re_showdoc.search(cell.source)
160167
def _add_directives(cell, d):
@@ -166,7 +173,7 @@ def clean_show_doc(cell):
166173
if not _is_showdoc(cell): return
167174
_add_directives(cell, {'output':'asis','echo':'false'})
168175

169-
# %% ../nbs/api/10_processors.ipynb 39
176+
# %% ../nbs/api/10_processors.ipynb 41
170177
def _ast_contains(trees, types):
171178
for tree in trees:
172179
for node in ast.walk(tree):
@@ -187,7 +194,7 @@ def _do_eval(cell):
187194
return True
188195
if _show_docs(trees): return True
189196

190-
# %% ../nbs/api/10_processors.ipynb 40
197+
# %% ../nbs/api/10_processors.ipynb 42
191198
class exec_show_docs(Processor):
192199
"Execute cells needed for `show_docs` output, including exported cells and imports"
193200
def begin(self):
@@ -214,13 +221,13 @@ def end(self):
214221
widgets = {**old, **new, 'state': {**old.get('state', {}), **new['state']}}
215222
self.nb.metadata['widgets'] = {mimetype: widgets}
216223

217-
# %% ../nbs/api/10_processors.ipynb 42
224+
# %% ../nbs/api/10_processors.ipynb 44
218225
def _import_obj(s):
219226
mod_nm, obj_nm = s.split(':')
220227
mod = importlib.import_module(mod_nm)
221228
return getattr(mod, obj_nm)
222229

223-
# %% ../nbs/api/10_processors.ipynb 43
230+
# %% ../nbs/api/10_processors.ipynb 45
224231
class FilterDefaults:
225232
"Override `FilterDefaults` to change which notebook processors are used"
226233
def xtra_procs(self):
@@ -230,7 +237,7 @@ def xtra_procs(self):
230237
def base_procs(self):
231238
return [FrontmatterProc, populate_language, add_show_docs, insert_warning,
232239
strip_ansi, hide_line, filter_stream_, rm_header_dash,
233-
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, strip_hidden_metadata]
240+
clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, strip_hidden_metadata]
234241

235242
def procs(self):
236243
"Processors for export"

nbs/api/10_processors.ipynb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@
215215
" shown_docs = {_get_nm(t) for t in _show_docs(trees)}\n",
216216
" for cell in reversed(exports):\n",
217217
" if cell_lang(cell) != 'python': raise ValueError(f\"{cell.metadata.language} can't export:\\n{cell.source}\")\n",
218-
" for nm in _def_names(cell, shown_docs): nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))\n",
218+
" nms = _def_names(cell, shown_docs)\n",
219+
" for nm in nms: nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))\n",
219220
" nb.has_docs_ = shown_docs or exports"
220221
]
221222
},
@@ -305,6 +306,31 @@
305306
"assert \"And not a link to <code>dict2nb</code>.\" in res"
306307
]
307308
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"id": "809e9225",
313+
"metadata": {},
314+
"outputs": [],
315+
"source": [
316+
"#|export\n",
317+
"def add_fold(cell):\n",
318+
" \"Add `code-fold` to `exports` cells\"\n",
319+
" if cell.cell_type != 'code' or 'exports' not in cell.directives_: return\n",
320+
" cell.source = f'#| code-fold: show\\n#| code-summary: \"Exported source\"\\n{cell.source}'"
321+
]
322+
},
323+
{
324+
"cell_type": "code",
325+
"execution_count": null,
326+
"id": "bcb50c88",
327+
"metadata": {},
328+
"outputs": [],
329+
"source": [
330+
"res = _run_procs(add_fold)\n",
331+
"assert \"#| code-fold: show\" in res"
332+
]
333+
},
308334
{
309335
"cell_type": "markdown",
310336
"id": "463b9def-91ad-4b05-92c3-e074954e4faf",
@@ -655,7 +681,7 @@
655681
" def base_procs(self):\n",
656682
" return [FrontmatterProc, populate_language, add_show_docs, insert_warning,\n",
657683
" strip_ansi, hide_line, filter_stream_, rm_header_dash,\n",
658-
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, strip_hidden_metadata]\n",
684+
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, strip_hidden_metadata]\n",
659685
"\n",
660686
" def procs(self):\n",
661687
" \"Processors for export\"\n",
@@ -686,11 +712,19 @@
686712
"#|hide\n",
687713
"import nbdev; nbdev.nbdev_export()"
688714
]
715+
},
716+
{
717+
"cell_type": "code",
718+
"execution_count": null,
719+
"id": "c35ef8df",
720+
"metadata": {},
721+
"outputs": [],
722+
"source": []
689723
}
690724
],
691725
"metadata": {
692726
"kernelspec": {
693-
"display_name": "python3",
727+
"display_name": "Python 3 (ipykernel)",
694728
"language": "python",
695729
"name": "python3"
696730
}

nbs/tutorials/best_practices.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,9 +1467,9 @@
14671467
],
14681468
"metadata": {
14691469
"kernelspec": {
1470-
"display_name": "aa",
1470+
"display_name": "Python 3 (ipykernel)",
14711471
"language": "python",
1472-
"name": "aa"
1472+
"name": "python3"
14731473
}
14741474
},
14751475
"nbformat": 4,

tests/docs_test.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
"metadata": {},
439439
"outputs": [],
440440
"source": [
441-
"#|export\n",
441+
"#|exports\n",
442442
"class b(a): ..."
443443
]
444444
},

0 commit comments

Comments
 (0)