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 commented Apr 3, 2013

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

@marscher

This comment has been minimized.

Copy link

marscher commented Oct 15, 2014

thank you for this fine working snippet!

@wjv

This comment has been minimized.

Copy link

wjv commented Nov 13, 2014

I found this very helpful, thanks!

@marctrem

This comment has been minimized.

Copy link

marctrem commented Nov 25, 2014

Thanks!

@chryss

This comment has been minimized.

Copy link

chryss commented Apr 23, 2015

Thanks! Saved me writing this today :-).

@darrengarvey

This comment has been minimized.

Copy link

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 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 commented Jul 15, 2015

Works like a charm! Really appreciate the snippet.

@swnesbitt

This comment has been minimized.

Copy link

swnesbitt commented Aug 20, 2015

Doesn't work for version 4 notebook files.

@mansour2014

This comment has been minimized.

Copy link

mansour2014 commented Feb 6, 2016

It is very nice, thank you

@ApplyHiTech

This comment has been minimized.

Copy link

ApplyHiTech commented Mar 25, 2016

THANK YOU! SAVED ME A LOT OF TIME!

@kamran-haider

This comment has been minimized.

Copy link

kamran-haider commented Jun 30, 2016

Just came across this, awesome script. Thanks.

@mkhm

This comment has been minimized.

Copy link

mkhm commented Aug 14, 2016

Nice! thanks.

@jabellcu

This comment has been minimized.

Copy link

jabellcu commented Dec 13, 2016

Amazing! ^^

@jaderabbit

This comment has been minimized.

Copy link

jaderabbit commented Jan 31, 2017

Thank you!

@gidim

This comment has been minimized.

Copy link

gidim commented Apr 25, 2017

Thanks!

@brydavis

This comment has been minimized.

Copy link

brydavis commented Jul 22, 2017

Awesome, thanks

@ZEMUSHKA

This comment has been minimized.

Copy link

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 commented Dec 6, 2017

amazing!

@ssunkara1

This comment has been minimized.

Copy link

ssunkara1 commented Dec 15, 2017

Thank you! you are a life saver

@nsuh

This comment has been minimized.

Copy link

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 commented Apr 9, 2018

Thank you very much for this!

@mancap314

This comment has been minimized.

Copy link

mancap314 commented Jul 26, 2018

Thanks! You saved my day :)

@gs511

This comment has been minimized.

Copy link

gs511 commented Aug 8, 2018

thanks alooooot

@whyboris

This comment has been minimized.

Copy link

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

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