Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove cells from Jupyter Notebook with nbconvert

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?

like image 850
user764640 Avatar asked Oct 16 '18 11:10

user764640


People also ask

What is Nbconvert in Jupyter?

Primarily, the nbconvert tool allows you to convert a Jupyter . ipynb notebook document file into another static format including HTML, LaTeX, PDF, Markdown, reStructuredText, and more. nbconvert can also add productivity to your workflow when used to execute notebooks programmatically.

How do I delete cells in Ipython?

“delete a cell in jupyter notebook” Code Answer's D+D(press the key twice when you are in command mode) to delete the selected cell.

How do you use tags in Jupyter notebook?

The Jupyter Notebook ships with a cell tag editor by default. This lets you add cell tags to each cell quickly. To enable the cell tag editor, click View -> Cell Toolbar -> Tags . This will enable the tags UI.

How do you hide input cells in Jupyter notebook?

Hide cell inputs If you add the tag hide-input to a cell, then Jupyter Book will hide the cell but display the outputs.


2 Answers

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

like image 124
user6481870 Avatar answered Sep 30 '22 07:09

user6481870


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

like image 41
Artem Trunov Avatar answered Sep 29 '22 07:09

Artem Trunov