使用nbconvert从Jupyter Notebook中删除单元格

use*_*640 6 jupyter nbconvert

如何在IPython Notebook中隐藏一个特定的单元格(输入或输出)中提到的建议不工作

在Windows上,请执行以下操作

jupyter nbconvert a.ipynb --TagRemovePreprocessor.remove_cell_tags="{'remove_cell'}"
Run Code Online (Sandbox Code Playgroud)

但出现错误

traitlets.traitlets.TraitError: The 'remove_cell_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u"{'remove_cell'}") was specified.
Run Code Online (Sandbox Code Playgroud)

我也尝试过'{“ remove_cell”}'

我正在使用nbconvert 5.4.0

任何想法如何做到这一点?

Dou*_*eon 5

您需要在调用 TagRemovePreprocessor 之前启用它。

下面的代码显示了如何启用它以及如何将标签包含为列表,以便您可以根据需要排除多个标签。要排除单个标签,只需在列表中放入一个元素,例如 ['remove_cell']。

如果要转换为 html,则不需要参数 --to html(因为 html 是默认值)。例如,如果要转换为python,请将--to html更改为--to python

jupyter nbconvert a.ipynb --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags="['remove_cell', 'other_tag_to_remove']" --to html

请注意,TagRemovePreprocessor 仅在 nbconvert 5.3 及更高版本中可用: https: //nbconvert.readthedocs.io/en/latest/changelog.html ?highlight=TagRemovePreprocessor


Art*_*nov 3

需要一些额外的引用才能工作:

--TagRemovePreprocessor.remove_cell_tags={\"remove_cell\"}

但是,请注意笔记本电脑到笔记本电脑转换的持续问题 - 在这种情况下,预处理器(包括标签删除)似乎不会运行。在这个问题中查看更多内容:

jupyter nbconvert --到笔记本,不排除原始单元

更新:未在 Windows 上测试,仅在 Linux 上测试