removing certain cells via tags fails due to wrong type

我怕爱的太早我们不能终老 提交于 2019-12-11 00:49:03

问题


I want to hide certain parts of a jupyter notebook and came across tags which can achieve this. I've tagged the cells with remove_cell in my notebook an tried to run

$ jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="{'remove_cell'}" 

however I always get the following error:

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.

I've tried to change the "{'remove_cell'}" to various format, e.g. {'remove_cell'} etc with the same result. Any help would be appreciated


回答1:


According to nbconvert documentation it must be done as you have specified. But there seems to be some bug in command line parsing traitlets API, used internally by jupyter nbconvert. So i tried a slightly different approach of specifying Configuration in jupyter_nbconvert_config.py file.

Steps:

  1. jupyter nbconvert --generate-config This will generate default ~/.jupyter/jupyter_nbconvert_config.py.

  2. Edit the configuration file and specify your configuration, in this case c.TagRemovePreprocessor.remove_input_tags = set(['remove_cell'])

  3. Run jupyter nbconvert test.ipynb This will remove the tagged cells and convert it to default HTML page.



回答2:


I got this to work without editing the config file by using square brackets, which match the format of the tag in the cell metadata, instead of curly braces.

So, in my cell metadata I have this:

{
    "tags": ["remove_cell"]
}

and on the command line I used:

jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="['remove_cell']"

which successfully removed any cells with this tag from the HTML output.



来源:https://stackoverflow.com/questions/53443437/removing-certain-cells-via-tags-fails-due-to-wrong-type

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