def _run_procs(procs=None, return_nb=False, path=_test_file):
= NBProcessor(path, procs)
nbp
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。
= _run_procs(insert_warning)
res assert "<!-- WARNING: THIS FILE WAS AUTOGENERATED!" in res
'foo', None, 'a').filter(lambda x:x == 1)
L(= re.compile('a') _tstre
add_show_docs
add_show_docs (nb)
在导出的单元格后添加 show_doc 单元格,除非它们已经有文档
cell_lang
cell_lang (cell)
= _run_procs([populate_language, add_show_docs])
res assert "show_doc(some_func)'" in res
assert "show_doc(and_another)'" in res
assert "show_doc(another_func)'" not in res
fdiv
fdiv (attrs='')
在 quarto 中创建一个 fenced div markdown 单元格
= fdiv('.py-2')
a 'markdown')
test_eq(a.cell_type, '::: {.py-2}') test_eq(a.source,
boxify
boxify (cells)
在 cells
周围添加一个框
mv_exports
mv_exports (nb)
将 exports
单元格移动到 show_doc
之后
add_links
add_links (cell)
为 markdown 单元格添加链接
= _run_procs(add_links)
res 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 res
add_fold
add_fold (cell)
为 exports
单元格添加 code-fold
= _run_procs(add_fold)
res assert "#| code-fold: show" in res
去除从标准输出流出的颜色,这些颜色可能会干扰静态网站生成器
strip_ansi
strip_ansi (cell)
去除 Ansi 字符。
= _run_procs(strip_ansi)
res assert not _re_ansi_escape.findall(res)
hide_
hide_ (cell)
从输出中隐藏单元格
= _run_procs(hide_)
res assert 'you will not be able to see this cell at all either' not in res
hide_line
hide_line (cell)
在代码单元格中,通过在代码行末尾使用 hide_line
指令来隐藏代码行
= _run_procs(hide_line)
res assert r"def show():\n a = 2\n b = 3" not in res
assert r"def show():\n a = 2" in res
filter_stream_
filter_stream_ (cell, *words)
移除 cell
流输出中包含任意 words
的输出行
= _run_procs(filter_stream_)
res =r"'A line\n', 'Another line.\n'"
expassert exp in res
ai_magics
ai_magics (cell)
一个将 AI 魔法命令转换为 markdown 的预处理器
= _run_procs(ai_magics)
res assert "'source': 'This is a test.'" in res
clean_magics
clean_magics (cell)
一个移除单元格魔法命令的预处理器
= _run_procs(clean_magics)
res assert "%%" not in res
rm_header_dash
rm_header_dash (cell)
移除以破折号 - 结尾的标题
= _run_procs(rm_header_dash)
res assert 'some words' in res
assert 'A heading to Hide' not in res
assert 'Yet another heading to hide' not in res
rm_export
rm_export (cell)
移除已导出或隐藏的单元格
= _run_procs(rm_export)
res assert 'dontshow' not in res
clean_show_doc
clean_show_doc (cell)
移除 ShowDoc 输入单元格
exec_show_docs
exec_show_docs (nb)
执行 show_docs
输出所需的单元格,包括导出的单元格和导入
= _run_procs([add_show_docs, exec_show_docs])
res assert res
FilterDefaults
FilterDefaults ()
覆盖 FilterDefaults
以更改使用的 notebook 处理器