问题
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:
jupyter nbconvert --generate-configThis will generate default ~/.jupyter/jupyter_nbconvert_config.py.Edit the configuration file and specify your configuration, in this case
c.TagRemovePreprocessor.remove_input_tags = set(['remove_cell'])- Run
jupyter nbconvert test.ipynbThis 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