Skip to content

Instantly share code, notes, and snippets.

@damianavila
Created April 3, 2013 22:05
Show Gist options
  • Select an option

  • Save damianavila/5305869 to your computer and use it in GitHub Desktop.

Select an option

Save damianavila/5305869 to your computer and use it in GitHub Desktop.
Remove output from IPython notebook from the command line (dev version 1.0)
"""
Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ]
Modified from remove_output by Minrk
"""
import sys
import io
import os
from IPython.nbformat.current import read, write
def remove_outputs(nb):
"""remove the outputs from a notebook"""
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
cell.outputs = []
if __name__ == '__main__':
fname = sys.argv[1]
with io.open(fname, 'r') as f:
nb = read(f, 'json')
remove_outputs(nb)
base, ext = os.path.splitext(fname)
new_ipynb = "%s_removed%s" % (base, ext)
with io.open(new_ipynb, 'w', encoding='utf8') as f:
write(nb, f, 'json')
print "wrote %s" % new_ipynb
@darrengarvey
Copy link
Copy Markdown

Thanks for that. I'm surprised there isn't something built into the notebook itself, but this saved me the time figuring it out anyway. Cheers.

@tzaffi
Copy link
Copy Markdown

tzaffi commented Jun 16, 2015

This resulted in a broken notebook for me, but Min RK's original script still worked.

@Eric-Xu
Copy link
Copy Markdown

Eric-Xu commented Jul 15, 2015

Works like a charm! Really appreciate the snippet.

@swnesbitt
Copy link
Copy Markdown

Doesn't work for version 4 notebook files.

@mzmansour
Copy link
Copy Markdown

It is very nice, thank you

@ApplyHiTech
Copy link
Copy Markdown

THANK YOU! SAVED ME A LOT OF TIME!

@kamran-haider
Copy link
Copy Markdown

Just came across this, awesome script. Thanks.

@mkhm
Copy link
Copy Markdown

mkhm commented Aug 14, 2016

Nice! thanks.

@jabellcu
Copy link
Copy Markdown

Amazing! ^^

@jaderabbit
Copy link
Copy Markdown

Thank you!

@gidim
Copy link
Copy Markdown

gidim commented Apr 25, 2017

Thanks!

@brydavis
Copy link
Copy Markdown

Awesome, thanks

@ZEMUSHKA
Copy link
Copy Markdown

jupyter nbconvert in.ipynb --to notebook --ClearOutputPreprocessor.enabled=True --stdout > out.ipynb

@stevenvo
Copy link
Copy Markdown

stevenvo commented Dec 6, 2017

amazing!

@ssunkara1
Copy link
Copy Markdown

Thank you! you are a life saver

@nsuh
Copy link
Copy Markdown

nsuh commented Mar 8, 2018

I got the error
python clear_jupyter_notebook_output.py Untitled-Copy11.ipynb > Untitled-Copy11.ipynb
/Library/Python/2.7/site-packages/IPython/nbformat.py:13: ShimWarning: The IPython.nbformat package has been deprecated since IPython 4.0. You should import from nbformat instead.
"You should import from nbformat instead.", ShimWarning)
/Users/nsuh/Library/Python/2.7/lib/python/site-packages/nbformat/current.py:19: UserWarning: nbformat.current is deprecated.

  • use nbformat for read/write/validate public API
  • use nbformat.vX directly to composing notebooks of a particular version

Instead I would use https://stackoverflow.com/questions/28908319/how-to-clear-an-ipython-notebooks-output-in-all-cells-from-the-linux-terminal

@CEPav
Copy link
Copy Markdown

CEPav commented Apr 9, 2018

Thank you very much for this!

@mancap314
Copy link
Copy Markdown

mancap314 commented Jul 26, 2018

Thanks! You saved my day :)

@gs511
Copy link
Copy Markdown

gs511 commented Aug 8, 2018

thanks alooooot

@whyboris
Copy link
Copy Markdown

whyboris commented Aug 27, 2018

Thank you @nsuh for the link to use Jupyter's nbconvert:
https://stackoverflow.com/questions/28908319/how-to-clear-an-ipython-notebooks-output-in-all-cells-from-the-linux-terminal

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb

@KwatMDPhD
Copy link
Copy Markdown

Use clean_ipynb, which not only clears notebook output but can also clean the code.

Install by pip install clean_ipynb

Run by clean_ipynb hello.ipynb

@eamtalu
Copy link
Copy Markdown

eamtalu commented Mar 21, 2019

Use clean_ipynb, which not only clears notebook output but can also clean the code.

Install by pip install clean_ipynb

Run by clean_ipynb hello.ipynb

Wow,,, this works like a majic for me...saves me from lots of hardle... I was having issues dealing with my jupyter lab and it was not opening. After cleaning up the output, jupyter lab started working fine..
Great tips.

@ibozkurt79
Copy link
Copy Markdown

@ KwatME you saved me from hell. A work of a weekend

@andreipit
Copy link
Copy Markdown

Thank! This helped me to open notebook.ipynb with 1,000,000 lines of output!

@waleedsial
Copy link
Copy Markdown

Use clean_ipynb, which not only clears notebook output but can also clean the code.

Install by pip install clean_ipynb

Run by clean_ipynb hello.ipynb

This is best.

@aaronlelevier
Copy link
Copy Markdown

The last line is Python2 specific. It could be made Python2/3 compatible by using

from __future__ import print_function
# existing code
print("wrote %s" % new_ipynb)

@mathematicalmichael
Copy link
Copy Markdown

Hey!
Thanks for this, it put me on the right track. In my case, history was being clogged up with 10's to 100's of MBs for a file that is supposed to be under 50KB. I removed history and all cell outputs and found the file to be back to its correct size.

The nuclear option....

Here's my update for Python 3:
https://gist.github.com/mathematicalmichael/a206b2a21de0bf88a5703e8700403019

@JyothsnaGH
Copy link
Copy Markdown

this worked for me a few months back, but now I get the following error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1843494: character maps to

error is thrown for nb =read(f, 'json')

@kylo2001
Copy link
Copy Markdown

I love it..
thank you dude

@emskiphoto
Copy link
Copy Markdown

I wasn't able to get this version or @mathematicalmichael version to work due to errors.

I was able to recover my 75 MB .ipynb file that was too large to load in Jupyter notebook on Chrome. I made the mistake of printing a gigantic list to the notebook output and saving the .ipynb. The solution was to open the .ipynb file with a simple text editor and delete the gigantic list output and this dropped the file size to 3 MB. Now it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment