Remove cells from Jupyter Notebook with nbconvert

孤街浪徒 提交于 2019-12-06 01:24:13

问题


Recommendations mentioned in How to hide one specific cell (input or output) in IPython Notebook? don't work.

On Windows I do the following

jupyter nbconvert a.ipynb --TagRemovePreprocessor.remove_cell_tags="{'remove_cell'}"

but get an error

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.

I also tried '{"remove_cell"}'

I am using nbconvert 5.4.0

Any ideas how to do this?


回答1:


You need to enable the TagRemovePreprocessor before you call it.

The code below shows how to enable it and how to enclose your tags as a list so you can exclude more than one tag if you wish. To exclude a single tag, just put one element in the list eg ['remove_cell'].

The parameter --to html is not required if you are converting to html (as html is the default). If you want to convert to python, for example, change --to html to --to python

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

Note that the TagRemovePreprocessor is only available in nbconvert 5.3 and above: https://nbconvert.readthedocs.io/en/latest/changelog.html?highlight=TagRemovePreprocessor




回答2:


Needs some extra quoting to work:

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

However beware of an ongoing issue with noteboot to notebook conversion - it seems like in this case preprocessors, including tag removal, do not run. See more in this SO question:

jupyter nbconvert --to notebook not excluding raw cells

Update: Not tested on windows, just on Linux



来源:https://stackoverflow.com/questions/52834910/remove-cells-from-jupyter-notebook-with-nbconvert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!