Remove cells from Jupyter Notebook with nbconvert

2020-07-09 10: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?

2条回答
【Aperson】
2楼-- · 2020-07-09 10:45

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

查看更多
放荡不羁爱自由
3楼-- · 2020-07-09 10:59

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

查看更多
登录 后发表回答