= read_nb('../../tests/image.ipynb')
test_nb assert test_nb.cells[0].outputs[0].data['image/png'][-1] == "\n" # Make sure it was not converted by acccident
clean_nb(test_nb)assert test_nb.cells[0].outputs[0].data['image/png'][-1] != "\n"
清洗
为避免在使用 jupyter notebook 时(由于执行次数或单元格元数据不同)出现无谓的冲突,建议在提交任何内容之前清洗 notebook(如果使用 nbdev_install_hooks
安装了 git 钩子,这将自动完成)。以下函数用于执行此操作。
信任
nbdev_trust
nbdev_trust (fname:str=None, force_all:bool=False)
信任匹配 fname
的 notebook
类型 | 默认值 | 详情 | |
---|---|---|---|
fname | str | None | 要信任的 notebook 名称或 glob 模式 |
force_all | bool | False | 同时信任未更改的 notebook |
清洗
clean_nb
clean_nb (nb, clear_all=False, allowed_metadata_keys:list=None, allowed_cell_metadata_keys:list=None, clean_ids=True)
从 nb
中清除多余的元数据
类型 | 默认值 | 详情 | |
---|---|---|---|
nb | 要清洗的 notebook | ||
clear_all | bool | False | 移除所有单元格元数据和单元格输出? |
allowed_metadata_keys | list | None | 保留主 notebook 元数据中的键列表 |
allowed_cell_metadata_keys | list | None | 保留单元格级别元数据中的键列表 |
clean_ids | bool | True | 从纯文本 repr 中移除 id? |
Jupyter 在单元格输出的图像中添加了一个尾随标签。Vscode-jupyter 则不添加。
Notebook 应统一风格以避免不必要的差异
测试用的 notebook 在主元数据部分和第二个单元格中都包含单元格级别的元数据
= read_nb('../../tests/metadata.ipynb')
test_nb
assert {'meta', 'jekyll', 'my_extra_key', 'my_removed_key'} <= test_nb.metadata.keys()
assert {'meta', 'hide_input', 'my_extra_cell_key', 'my_removed_cell_key'} == test_nb.cells[1].metadata.keys()
清洗 notebook 后,所有多余的元数据都会被移除,默认只允许保留部分键
clean_nb(test_nb)
assert {'jekyll', 'kernelspec'} == test_nb.metadata.keys()
assert {'hide_input'} == test_nb.cells[1].metadata.keys()
我们可以在 notebook 或单元格级别保留一些额外的键
= read_nb('../../tests/metadata.ipynb')
test_nb ={'my_extra_key'}, allowed_cell_metadata_keys={'my_extra_cell_key'})
clean_nb(test_nb, allowed_metadata_keys
assert {'jekyll', 'kernelspec', 'my_extra_key'} == test_nb.metadata.keys()
assert {'hide_input', 'my_extra_cell_key'} == test_nb.cells[1].metadata.keys()
传递 clear_all=True
会移除单元格元数据中的所有内容
= read_nb('../../tests/metadata.ipynb')
test_nb =True)
clean_nb(test_nb, clear_all
assert {'jekyll', 'kernelspec'} == test_nb.metadata.keys()
1].metadata, {}) test_eq(test_nb.cells[
传递 clean_ids=True
会从纯文本 repr 输出中移除 id
,以避免 notebook 内容因每次运行而改变,因为这通常会导致 git 合并冲突。例如
<PIL.PngImagePlugin.PngImageFile image mode=L size=28x28 at 0x7FB4F8979690>
变成
<PIL.PngImagePlugin.PngImageFile image mode=L size=28x28>
process_write
process_write (warn_msg, proc_nb, f_in, f_out=None, disp=False)
nbdev_clean
nbdev_clean (fname:str=None, clear_all:bool=False, disp:bool=False, stdin:bool=False)
清洗 fname
中的所有 notebook 以避免合并冲突
类型 | 默认值 | 详情 | |
---|---|---|---|
fname | str | None | 要清洗的 notebook 名称或 glob 模式 |
clear_all | bool | False | 移除所有单元格元数据和单元格输出? |
disp | bool | False | 打印清洗后的输出 |
stdin | bool | False | 从输入流读取 notebook |
默认情况下(fname
留空或设为 None
),会清洗 config.nbs_path
中的所有 notebook。您可以通过传递 clear_all=True
来选择完全清洗 notebook,移除所有元数据和单元格输出。
如果您想在主 notebook 元数据中保留一些键,可以在 settings.ini
中设置 allowed_metadata_keys
。对于单元格级别的元数据,类似地使用 allowed_cell_metadata_keys
。例如,要在 notebook 和单元格级别都保留 k1
和 k2
,可以在 settings.ini
中添加以下内容
...
allowed_metadata_keys = k1 k2
allowed_cell_metadata_keys = k1 k2
...
clean_jupyter
clean_jupyter (path, model, **kwargs)
在保存到 path
之前清洗 Jupyter model
这会在保存时清洗 notebook,以避免不必要的合并冲突。在 Jupyter Notebook 和 Lab 中安装它的最简单方法是运行 nbdev_install_hooks
。它通过实现 Jupyter 文件保存钩子 API 中的 pre_save_hook
来工作。
钩子
nbdev_install_hooks
nbdev_install_hooks ()
安装 Jupyter 和 git 钩子,自动清洗、信任和修复 notebook 中的合并冲突
有关每个钩子如何工作的更多信息,请参阅 clean_jupyter
和 nbdev_merge
。