Create a gist now

Instantly share code, notes, and snippets.

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.

Show comment Hide comment
@leto

leto Apr 3, 2013

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

leto commented Apr 3, 2013

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

@marscher

This comment has been minimized.

Show comment Hide comment
@marscher

marscher Oct 15, 2014

thank you for this fine working snippet!

thank you for this fine working snippet!

@wjv

This comment has been minimized.

Show comment Hide comment
@wjv

wjv Nov 13, 2014

I found this very helpful, thanks!

wjv commented Nov 13, 2014

I found this very helpful, thanks!

@marctrem

This comment has been minimized.

Show comment Hide comment
@marctrem

marctrem Nov 25, 2014

Thanks!

Thanks!

@chryss

This comment has been minimized.

Show comment Hide comment
@chryss

chryss Apr 23, 2015

Thanks! Saved me writing this today :-).

chryss commented Apr 23, 2015

Thanks! Saved me writing this today :-).

@darrengarvey

This comment has been minimized.

Show comment Hide comment
@darrengarvey

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

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.

Show comment Hide comment
@tzaffi

tzaffi Jun 16, 2015

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

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.

Show comment Hide comment
@Eric-Xu

Eric-Xu Jul 15, 2015

Works like a charm! Really appreciate the snippet.

Eric-Xu commented Jul 15, 2015

Works like a charm! Really appreciate the snippet.

@swnesbitt

This comment has been minimized.

Show comment Hide comment
@swnesbitt

swnesbitt Aug 20, 2015

Doesn't work for version 4 notebook files.

Doesn't work for version 4 notebook files.

@mansour2014

This comment has been minimized.

Show comment Hide comment
@mansour2014

mansour2014 Feb 6, 2016

It is very nice, thank you

It is very nice, thank you

@ApplyHiTech

This comment has been minimized.

Show comment Hide comment
@ApplyHiTech

ApplyHiTech Mar 25, 2016

THANK YOU! SAVED ME A LOT OF TIME!

THANK YOU! SAVED ME A LOT OF TIME!

@kamran-haider

This comment has been minimized.

Show comment Hide comment
@kamran-haider

kamran-haider Jun 30, 2016

Just came across this, awesome script. Thanks.

Just came across this, awesome script. Thanks.

@mkhm

This comment has been minimized.

Show comment Hide comment
@mkhm

mkhm Aug 14, 2016

Nice! thanks.

mkhm commented Aug 14, 2016

Nice! thanks.

@jabellcu

This comment has been minimized.

Show comment Hide comment
@jabellcu

jabellcu Dec 13, 2016

Amazing! ^^

Amazing! ^^

@jaderabbit

This comment has been minimized.

Show comment Hide comment
@jaderabbit

jaderabbit Jan 31, 2017

Thank you!

Thank you!

@gidim

This comment has been minimized.

Show comment Hide comment
@gidim

gidim Apr 25, 2017

Thanks!

gidim commented Apr 25, 2017

Thanks!

@brydavis

This comment has been minimized.

Show comment Hide comment
@brydavis

brydavis Jul 22, 2017

Awesome, thanks

Awesome, thanks

@ZEMUSHKA

This comment has been minimized.

Show comment Hide comment
@ZEMUSHKA

ZEMUSHKA Sep 16, 2017

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

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

@stevenvo

This comment has been minimized.

Show comment Hide comment
@stevenvo

stevenvo Dec 6, 2017

amazing!

stevenvo commented Dec 6, 2017

amazing!

@ssunkara1

This comment has been minimized.

Show comment Hide comment
@ssunkara1

ssunkara1 Dec 15, 2017

Thank you! you are a life saver

Thank you! you are a life saver

@nsuh

This comment has been minimized.

Show comment Hide comment
@nsuh

nsuh 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

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.

Show comment Hide comment
@CEPav

CEPav Apr 9, 2018

Thank you very much for this!

CEPav commented Apr 9, 2018

Thank you very much for this!

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