Parente's Mindtrove

Jupyter Tidbit: Use nbconvert to clear notebook outputs

August 22, 2018

This post originates from a gist that supports comments and forks.


Summary

nbconvert has a preprocessor that clears cell outputs from notebook files, leaving cell inputs intact.

Example

The following shell command reads my_input_notebook.ipynb, removes its cell outputs, prints the cleaned notebook to stdout, and redirects that output to a new notebook file named my_output_notebook.ipynb.

jupyter nbconvert my_input_notebook.ipynb --to notebook --ClearOutputPreprocessor.enabled=True --stdout > my_output_notebook.ipynb

The following command works similiarly, but without using a redirect.

jupyter nbconvert my_input_notebook.ipynb --to notebook --ClearOutputPreprocessor.enabled=True --output my_output_notebook

Why is this useful?

One day, you may run a notebook, walk away, and return later to find your notebook has produced so much output that it has brought your browser to its knees ... after autosaving. You'll try to reload that notebook to see what went wrong, only to find that your browser tab slows to a crawl and crashes once again. Clearing outputs at the command line is one way to get out of this sticky situation.

On a lighter note, you may just want to quickly purge your notebook of output before sharing it, without having to open it in Jupyter Notebook, JupyterLab, etc.

Another Read: Jupyter Tidbit: %run your ipynb file »

The %run magic provided by IPython not only supports the execution of regular Python scripts, it also runs Jupyter Notebook files (.ipynb).