def _run_procs(procs=None, return_nb=False, path=_test_file):
nbp = NBProcessor(path, procs)
nbp.process()
if return_nb: return nbp.nb
return '\n'.join([str(cell) for cell in nbp.nb.cells])处理器
NBProcessor 的一些处理器
在本页中,我们将使用这个私有辅助函数来处理 notebook 并返回结果,以简化测试
populate_language
populate_language (nb)
根据 NB 元数据和魔法命令设置单元格语言
insert_warning
insert_warning (nb)
在第一个单元格后向 Notebook 插入自动生成警告。
这个预处理器会在 markdown 目的地插入一个警告,说明该文件是自动生成的。这个警告被插入到第二个单元格,这样就不会干扰 front matter。
res = _run_procs(insert_warning)
assert "<!-- WARNING: THIS FILE WAS AUTOGENERATED!" in resL('foo', None, 'a').filter(lambda x:x == 1)
_tstre = re.compile('a')add_show_docs
add_show_docs (nb)
在导出的单元格后添加 show_doc 单元格,除非它们已经有文档
cell_lang
cell_lang (cell)
res = _run_procs([populate_language, add_show_docs])
assert "show_doc(some_func)'" in res
assert "show_doc(and_another)'" in res
assert "show_doc(another_func)'" not in resfdiv
fdiv (attrs='')
在 quarto 中创建一个 fenced div markdown 单元格
a = fdiv('.py-2')
test_eq(a.cell_type, 'markdown')
test_eq(a.source, '::: {.py-2}')boxify
boxify (cells)
在 cells 周围添加一个框
mv_exports
mv_exports (nb)
将 exports 单元格移动到 show_doc 之后
add_links
add_links (cell)
为 markdown 单元格添加链接
res = _run_procs(add_links)
assert "[`numpy.array`](https://numpy.com.cn/doc/stable/reference/generated/numpy.array.html#numpy.array)" in res
assert "[`ModuleMaker`](https://nbdev.fastai.net.cn/api/maker.html#modulemaker) but not a link to `foobar`." in res
assert "A link in a docstring: [`ModuleMaker`](https://nbdev.fastai.net.cn/api/maker.html#modulemaker)." in res
assert "And not a link to <code>dict2nb</code>." in resadd_fold
add_fold (cell)
为 exports 单元格添加 code-fold
res = _run_procs(add_fold)
assert "#| code-fold: show" in res去除从标准输出流出的颜色,这些颜色可能会干扰静态网站生成器
strip_ansi
strip_ansi (cell)
去除 Ansi 字符。
res = _run_procs(strip_ansi)
assert not _re_ansi_escape.findall(res)hide_
hide_ (cell)
从输出中隐藏单元格
res = _run_procs(hide_)
assert 'you will not be able to see this cell at all either' not in reshide_line
hide_line (cell)
在代码单元格中,通过在代码行末尾使用 hide_line 指令来隐藏代码行
res = _run_procs(hide_line)
assert r"def show():\n a = 2\n b = 3" not in res
assert r"def show():\n a = 2" in resfilter_stream_
filter_stream_ (cell, *words)
移除 cell 流输出中包含任意 words 的输出行
res = _run_procs(filter_stream_)
exp=r"'A line\n', 'Another line.\n'"
assert exp in resai_magics
ai_magics (cell)
一个将 AI 魔法命令转换为 markdown 的预处理器
res = _run_procs(ai_magics)
assert "'source': 'This is a test.'" in resclean_magics
clean_magics (cell)
一个移除单元格魔法命令的预处理器
res = _run_procs(clean_magics)
assert "%%" not in resrm_header_dash
rm_header_dash (cell)
移除以破折号 - 结尾的标题
res = _run_procs(rm_header_dash)
assert 'some words' in res
assert 'A heading to Hide' not in res
assert 'Yet another heading to hide' not in resrm_export
rm_export (cell)
移除已导出或隐藏的单元格
res = _run_procs(rm_export)
assert 'dontshow' not in resclean_show_doc
clean_show_doc (cell)
移除 ShowDoc 输入单元格
exec_show_docs
exec_show_docs (nb)
执行 show_docs 输出所需的单元格,包括导出的单元格和导入
res = _run_procs([add_show_docs, exec_show_docs])
assert resFilterDefaults
FilterDefaults ()
覆盖 FilterDefaults 以更改使用的 notebook 处理器