Removing cells, inputs, or outputs#
When converting Notebooks into other formats, it is possible to remove parts of a cell, or entire cells, using preprocessors. The notebook will remain unchanged, but the outputs will have certain pieces removed. Here are two primary ways to accomplish this.
Removing cells using Regular Expressions on cell content#
Sometimes you’d rather remove cells based on their _content_ rather
than their tags. In this case, you can use the RegexRemovePreprocessor
.
You initialize this preprocessor with a single patterns
configuration, which
is a list of strings. For each cell, this preprocessor checks whether
the cell contents match any of the strings provided in patterns
.
If the contents match any of the patterns, the cell is removed from the notebook.
For example, execute the following command to convert a notebook to html and remove cells containing only whitespace:
jupyter nbconvert --RegexRemovePreprocessor.patterns="['\s*\Z']" mynotebook.ipynb
The command line argument sets the list of patterns to '\s*\Z'
which matches
an arbitrary number of whitespace characters followed by the end of the string.
See https://regex101.com/ for an interactive guide to regular expressions (make sure to select the python flavor). See https://docs.python.org/library/re.html for the official regular expression documentation in python.