math
math

Reputation: 2022

removing certain cells via tags fails due to wrong type

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

Upvotes: 5

Views: 1610

Answers (3)

aaak
aaak

Reputation: 1

nbconvert did not hide the cells with tags while also executing the notebook, and this is the most useful example I found here https://github.com/jupyter/nbconvert/issues/1300: So the underlying issue is that the preprocessors being added on your behalf are un-ordered, so by unlikely chance the --execute turning on the ExecutePreprocessor is getting applied after the TagRemovePreprocessor. To fix this you can set the preprocessors to use in an explicit order by doing:

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

Upvotes: 0

whopper510
whopper510

Reputation: 487

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.

Upvotes: 2

Sangram Gaikwad
Sangram Gaikwad

Reputation: 804

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.

Upvotes: 4

Related Questions