Skip to content

Commit

Permalink
added apps/notebooks/nbstripout from https://gist.github.com/minrk/61…
Browse files Browse the repository at this point in the history
  • Loading branch information
rjleveque committed Sep 27, 2014
1 parent 67d671f commit edeadb2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions notebooks/nbstripout
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
"""strip outputs from an IPython Notebook
Opens a notebook, strips its output, and writes the outputless version to the original file.
Useful mainly as a git pre-commit hook for users who don't want to track output in VCS.
This does mostly the same thing as the `Clear All Output` command in the notebook UI.
From: https://gist.github.com/minrk/6176788
"""

import io
import sys

from IPython.nbformat import current

def strip_output(nb):
"""strip the outputs from a notebook object"""
nb.metadata.pop('signature', None)
for cell in nb.worksheets[0].cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'prompt_number' in cell:
cell['prompt_number'] = None
return nb

if __name__ == '__main__':
filename = sys.argv[1]
with io.open(filename, 'r', encoding='utf8') as f:
nb = current.read(f, 'json')
nb = strip_output(nb)
with io.open(filename, 'w', encoding='utf8') as f:
current.write(nb, f, 'json')

0 comments on commit edeadb2

Please sign in to comment.