Skip to content

Instantly share code, notes, and snippets.

@drorata
Created March 12, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drorata/f3beb1ae736890b049f6 to your computer and use it in GitHub Desktop.
Save drorata/f3beb1ae736890b049f6 to your computer and use it in GitHub Desktop.
Remove output cells and execution counts
"""
Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ]
Modified from remove_output by:
damianavila (https://gist.github.com/damianavila/5305869)
"""
import sys
import io
import os
from IPython.nbformat import read, write
def remove_outputs(nb):
"""remove the outputs from a notebook"""
for cell in nb.cells:
if cell.cell_type == 'code':
cell.outputs = []
cell.execution_count = None
if __name__ == '__main__':
fname = sys.argv[1]
with io.open(fname, 'r') as f:
nb = read(f, 4)
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, 4)
print "wrote %s" % new_ipynb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment