Create a gist now

Instantly share code, notes, and snippets.

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

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

@marscher

thank you for this fine working snippet!

@wjv
wjv commented Nov 13, 2014

I found this very helpful, thanks!

@marctrem

Thanks!

@chryss
chryss commented Apr 23, 2015

Thanks! Saved me writing this today :-).

@darrengarvey

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

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

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

Works like a charm! Really appreciate the snippet.

@swnesbitt

Doesn't work for version 4 notebook files.

@mansour2014

It is very nice, thank you

@ApplyHiTech

THANK YOU! SAVED ME A LOT OF TIME!

@kamran-haider

Just came across this, awesome script. Thanks.

@mkhm
mkhm commented Aug 14, 2016

Nice! thanks.

@jabellcu

Amazing! ^^

@jaderabbit

Thank you!

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