Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@leto

This comment has been minimized.

Copy link

@leto leto commented Apr 3, 2013

Just for clarification, what is the license of this code?

@marscher

This comment has been minimized.

Copy link

@marscher marscher commented Oct 15, 2014

thank you for this fine working snippet!

@wjv

This comment has been minimized.

Copy link

@wjv wjv commented Nov 13, 2014

I found this very helpful, thanks!

@marctrem

This comment has been minimized.

Copy link

@marctrem marctrem commented Nov 25, 2014

Thanks!

@chryss

This comment has been minimized.

Copy link

@chryss chryss commented Apr 23, 2015

Thanks! Saved me writing this today :-).

@darrengarvey

This comment has been minimized.

Copy link

@darrengarvey darrengarvey commented May 15, 2015

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

This comment has been minimized.

Copy link

@tzaffi tzaffi commented Jun 16, 2015

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

@Eric-Xu

This comment has been minimized.

Copy link

@Eric-Xu Eric-Xu commented Jul 15, 2015

Works like a charm! Really appreciate the snippet.

@swnesbitt

This comment has been minimized.

Copy link

@swnesbitt swnesbitt commented Aug 20, 2015

Doesn't work for version 4 notebook files.

@mzmansour

This comment has been minimized.

Copy link

@mzmansour mzmansour commented Feb 6, 2016

It is very nice, thank you

@ApplyHiTech

This comment has been minimized.

Copy link

@ApplyHiTech ApplyHiTech commented Mar 25, 2016

THANK YOU! SAVED ME A LOT OF TIME!

@kamran-haider

This comment has been minimized.

Copy link

@kamran-haider kamran-haider commented Jun 30, 2016

Just came across this, awesome script. Thanks.

@mkhm

This comment has been minimized.

Copy link

@mkhm mkhm commented Aug 14, 2016

Nice! thanks.

@jabellcu

This comment has been minimized.

Copy link

@jabellcu jabellcu commented Dec 13, 2016

Amazing! ^^

@jaderabbit

This comment has been minimized.

Copy link

@jaderabbit jaderabbit commented Jan 31, 2017

Thank you!

@gidim

This comment has been minimized.

Copy link

@gidim gidim commented Apr 25, 2017

Thanks!

@brydavis

This comment has been minimized.

Copy link

@brydavis brydavis commented Jul 22, 2017

Awesome, thanks

@ZEMUSHKA

This comment has been minimized.

Copy link

@ZEMUSHKA ZEMUSHKA commented Sep 16, 2017

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

@stevenvo

This comment has been minimized.

Copy link

@stevenvo stevenvo commented Dec 6, 2017

amazing!

@ssunkara1

This comment has been minimized.

Copy link

@ssunkara1 ssunkara1 commented Dec 15, 2017

Thank you! you are a life saver

@nsuh

This comment has been minimized.

Copy link

@nsuh 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

This comment has been minimized.

Copy link

@CEPav CEPav commented Apr 9, 2018

Thank you very much for this!

@mancap314

This comment has been minimized.

Copy link

@mancap314 mancap314 commented Jul 26, 2018

Thanks! You saved my day :)

@gs511

This comment has been minimized.

Copy link

@gs511 gs511 commented Aug 8, 2018

thanks alooooot

@whyboris

This comment has been minimized.

Copy link

@whyboris 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

@KwatME

This comment has been minimized.

Copy link

@KwatME KwatME commented Dec 4, 2018

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

This comment has been minimized.

Copy link

@eamtalu 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

This comment has been minimized.

Copy link

@ibozkurt79 ibozkurt79 commented Apr 22, 2019

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

@andreipit

This comment has been minimized.

Copy link

@andreipit andreipit commented May 29, 2019

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

@waleedsial

This comment has been minimized.

Copy link

@waleedsial waleedsial commented Sep 11, 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

This is best.

@aaronlelevier

This comment has been minimized.

Copy link

@aaronlelevier aaronlelevier commented Sep 27, 2019

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

This comment has been minimized.

Copy link

@mathematicalmichael mathematicalmichael commented Feb 4, 2020

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

This comment has been minimized.

Copy link

@JyothsnaGH JyothsnaGH commented Jun 9, 2020

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.