Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear Jupyter Notebook's output in all cells from the Linux terminal?

I have a problem when the output from a notebook is really long and it's saved into the notebook, any time I want to open this particular notebook again the browser crashes and can't display correctly.

To fix this I have to open it with a text editor and delete all output from that cell causing the problem.

I wonder if there is a way to clean all output from the notebook so one can open it again without problem. I want to delete all output since deleting a specific one seems more troublesome.

like image 240
Diego Rueda Avatar asked Mar 06 '15 21:03

Diego Rueda


People also ask

How do you clear all cell output in jupyter notebook?

When you have Jupyter notebook opened, you can do this by selecting the Cell -> All Output -> Clear menu item.

How do I clear the memory on my jupyter notebook?

Jupyter Lesson 10: How to reset the Kernel (Clear all in memory objects and stop the code) If the notebook or your code is acting weird sometimes it is best to press the “RESET BUTTON”. Reseting the kernel clears all in memory objects and restarts your code from the very top.

How do I turn off output in Jupyter?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do I delete all cells in Google Colab?

Insert code cell above: ctrl + m + a. Insert code cell below: ctrl + m + b. Delete cell: ctrl + m + d.


2 Answers

nbconvert 6.0 should fix --clear-output

The option had been broken for a long time previously, bug report with merged patch: https://github.com/jupyter/nbconvert/issues/822

Usage should be for in-place operation:

jupyter nbconvert --clear-output --inplace my_notebook.ipynb 

Or to save to another file called my_notebook_no_out.ipynb:

jupyter nbconvert --clear-output \   --to notebook --output=my_notebook_no_out my_notebook.ipynb 

This was brought to my attention by Harold in the comments.

Before nbconvert 6.0: --ClearOutputPreprocessor.enabled=True

Same usage as --clear-output:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace my_notebook.ipynb jupyter nbconvert --ClearOutputPreprocessor.enabled=True \   --to notebook --output=my_notebook_no_out my_notebook.ipynb 

Tested in Jupyter 4.4.0, notebook==5.7.6.


If you create a .gitattributes file, you can run a filter over certain files before they are added to git. This will leave the original file on disk as-is, but commit the "cleaned" version.

For this to work, add this to your local .git/config or global ~/.gitconfig:

[filter "strip-notebook-output"]     clean = "jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to=notebook --stdin --stdout --log-level=ERROR" 

Then create a .gitattributes file in your directory with notebooks, with this content:

*.ipynb filter=strip-notebook-output 

How this works:

  • The attribute tells git to run the filter's clean action on each notebook file before adding it to the index (staging).
  • The filter is our friend nbconvert, set up to read from stdin, write to stdout, strip the output, and only speak when it has something important to say.
  • When a file is extracted from the index, the filter's smudge action is run, but this is a no-op as we did not specify it. You could run your notebook here to re-create the output (nbconvert --execute).
  • Note that if the filter somehow fails, the file will be staged unconverted.

My only minor gripe with this process is that I can commit .gitattributes but I have to tell my co-workers to update their .git/config.

If you want a hackier but much faster version, try JQ:

  clean = "jq '.cells[].outputs = [] | .cells[].execution_count = null | .'" 
like image 31
dirkjot Avatar answered Sep 28 '22 06:09

dirkjot