Skip to content

Commit

Permalink
fixes #1407
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Apr 28, 2024
1 parent 949b30f commit 3951d8a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
1 change: 1 addition & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
'nbdev.processors._re_hideline': ('api/processors.html#_re_hideline', 'nbdev/processors.py'),
'nbdev.processors._show_docs': ('api/processors.html#_show_docs', 'nbdev/processors.py'),
'nbdev.processors._want_doc': ('api/processors.html#_want_doc', 'nbdev/processors.py'),
'nbdev.processors.add_fold': ('api/processors.html#add_fold', 'nbdev/processors.py'),
'nbdev.processors.add_links': ('api/processors.html#add_links', 'nbdev/processors.py'),
'nbdev.processors.add_show_docs': ('api/processors.html#add_show_docs', 'nbdev/processors.py'),
'nbdev.processors.add_show_docs.begin': ( 'api/processors.html#add_show_docs.begin',
Expand Down
39 changes: 23 additions & 16 deletions nbdev/processors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/10_processors.ipynb.

# %% auto 0
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'add_links', 'strip_ansi',
__all__ = ['populate_language', 'insert_warning', 'cell_lang', 'add_show_docs', 'add_links', 'add_fold', 'strip_ansi',
'strip_hidden_metadata', 'hide_', 'hide_line', 'filter_stream_', 'clean_magics', 'rm_header_dash',
'rm_export', 'clean_show_doc', 'exec_show_docs', 'FilterDefaults']

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

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

# %% ../nbs/api/10_processors.ipynb 22
# %% ../nbs/api/10_processors.ipynb 21
def add_fold(cell):
"Add `code-fold` to `exports` cells"
if cell.cell_type != 'code' or 'exports' not in cell.directives_: return
cell.source = f'#| code-fold: show\n#| code-summary: "Exported source"\n{cell.source}'

# %% ../nbs/api/10_processors.ipynb 24
_re_ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')

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

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

# %% ../nbs/api/10_processors.ipynb 25
# %% ../nbs/api/10_processors.ipynb 27
def hide_(cell):
"Hide cell from output"
del(cell['source'])

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

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

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

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

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

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

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

# %% ../nbs/api/10_processors.ipynb 36
# %% ../nbs/api/10_processors.ipynb 38
_hide_dirs = {'export','exporti', 'hide','default_exp'}

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

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

# %% ../nbs/api/10_processors.ipynb 39
# %% ../nbs/api/10_processors.ipynb 41
def _ast_contains(trees, types):
for tree in trees:
for node in ast.walk(tree):
Expand All @@ -187,7 +194,7 @@ def _do_eval(cell):
return True
if _show_docs(trees): return True

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

# %% ../nbs/api/10_processors.ipynb 42
# %% ../nbs/api/10_processors.ipynb 44
def _import_obj(s):
mod_nm, obj_nm = s.split(':')
mod = importlib.import_module(mod_nm)
return getattr(mod, obj_nm)

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

def procs(self):
"Processors for export"
Expand Down
40 changes: 37 additions & 3 deletions nbs/api/10_processors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@
" shown_docs = {_get_nm(t) for t in _show_docs(trees)}\n",
" for cell in reversed(exports):\n",
" if cell_lang(cell) != 'python': raise ValueError(f\"{cell.metadata.language} can't export:\\n{cell.source}\")\n",
" for nm in _def_names(cell, shown_docs): nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))\n",
" nms = _def_names(cell, shown_docs)\n",
" for nm in nms: nb.cells.insert(cell.idx_+1, mk_cell(f'show_doc({nm})'))\n",
" nb.has_docs_ = shown_docs or exports"
]
},
Expand Down Expand Up @@ -305,6 +306,31 @@
"assert \"And not a link to <code>dict2nb</code>.\" in res"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "809e9225",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"def add_fold(cell):\n",
" \"Add `code-fold` to `exports` cells\"\n",
" if cell.cell_type != 'code' or 'exports' not in cell.directives_: return\n",
" cell.source = f'#| code-fold: show\\n#| code-summary: \"Exported source\"\\n{cell.source}'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb50c88",
"metadata": {},
"outputs": [],
"source": [
"res = _run_procs(add_fold)\n",
"assert \"#| code-fold: show\" in res"
]
},
{
"cell_type": "markdown",
"id": "463b9def-91ad-4b05-92c3-e074954e4faf",
Expand Down Expand Up @@ -655,7 +681,7 @@
" def base_procs(self):\n",
" return [FrontmatterProc, populate_language, add_show_docs, insert_warning,\n",
" strip_ansi, hide_line, filter_stream_, rm_header_dash,\n",
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, strip_hidden_metadata]\n",
" clean_show_doc, exec_show_docs, rm_export, clean_magics, hide_, add_links, add_fold, strip_hidden_metadata]\n",
"\n",
" def procs(self):\n",
" \"Processors for export\"\n",
Expand Down Expand Up @@ -686,11 +712,19 @@
"#|hide\n",
"import nbdev; nbdev.nbdev_export()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c35ef8df",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
}
Expand Down
4 changes: 2 additions & 2 deletions nbs/tutorials/best_practices.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "aa",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "aa"
"name": "python3"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion tests/docs_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"#|exports\n",
"class b(a): ..."
]
},
Expand Down

0 comments on commit 3951d8a

Please sign in to comment.