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

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

thank you for this fine working snippet!

wjv commented Nov 13, 2014

I found this very helpful, thanks!

Thanks!

chryss commented Apr 23, 2015

Thanks! Saved me writing this today :-).

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 commented Jun 16, 2015

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

Eric-Xu commented Jul 15, 2015

Works like a charm! Really appreciate the snippet.

Doesn't work for version 4 notebook files.

It is very nice, thank you

THANK YOU! SAVED ME A LOT OF TIME!

Just came across this, awesome script. Thanks.

mkhm commented Aug 14, 2016

Nice! thanks.

Amazing! ^^

Thank you!

gidim commented Apr 25, 2017

Thanks!

Awesome, thanks

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

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