我想隐藏jupyter笔记本的某些部分,并遇到了可以实现这一点的标签。我在笔记本上用remove_cell
标记了细胞,并尝试运行
$ jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="{'remove_cell'}"
然而,我总是得到以下错误:
traitlets.traitlets.TraitError: The 'remove_input_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u'{remove_cell}') was specified.
我尝试将"{'remove_cell'}"
更改为各种格式,例如{'remove_cell'}
等,结果相同。如有任何帮助,将不胜感激
根据nbconvert文档,必须按照您指定的方式进行。但在jupiter nbconvert内部使用的命令行解析traitlets
API中似乎存在一些错误。因此,我尝试了一种稍微不同的方法,在jupyter_nbconvert_config.py
文件中指定Configuration。
步骤:
-
jupyter nbconvert --generate-config
这将生成默认~/.jupyter/jupyter_nconvert_config.py. -
在这种情况下,编辑配置文件并指定您的配置
c.TagRemovePreprocessor.remove_input_tags = set(['remove_cell'])
- 运行
jupyter nbconvert test.ipynb
这将删除标记的单元格,并将其转换为默认的HTML页面
我在不编辑配置文件的情况下使用了方括号(与单元格元数据中标记的格式匹配(,而不是大括号。
所以,在我的单元格元数据中,我有这样的:
{
"tags": ["remove_cell"]
}
在命令行上,我使用了:
jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="['remove_cell']"
其成功地从HTML输出中移除了具有该标记的任何单元格。
nbconvert在执行笔记本时没有隐藏带有标记的单元格,这是我在这里找到的最有用的例子https://github.com/jupyter/nbconvert/issues/1300:因此,潜在的问题是,代表您添加的预处理器是无序的,因此打开ExecutePreprocessor的--execute在TagRemovePreprocessor之后应用的可能性很小。要解决此问题,您可以通过以下操作将预处理器设置为按显式顺序使用:
jupyter nbconvert notebooks/demo_notebook.ipynb
--TagRemovePreprocessor.remove_input_tags='{"remove-input"}'
--TagRemovePreprocessor.remove_all_outputs_tags='{"remove-output"}'
--Exporter.preprocessors='["nbconvert.preprocessors.ExecutePreprocessor","nbconvert.preprocessors.TagRemovePreprocessor"]'
--to notebook