Skip to content

Instantly share code, notes, and snippets.

@oesteban
Forked from damianavila/remove_output.py
Created June 6, 2016 18:51
Show Gist options
  • Save oesteban/6ae26a337b291f0a8db583241a9d6d44 to your computer and use it in GitHub Desktop.
Save oesteban/6ae26a337b291f0a8db583241a9d6d44 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment