-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbstripout
executable file
·33 lines (24 loc) · 1005 Bytes
/
nbstripout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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.
Adapted from rom https://gist.github.com/minrk/6176788 to work with
git filter driver
"""
import sys
#You may need to do this for your script to work with GitX or Tower:
#sys.path.append("/Users/chris/anaconda/envs/conda/lib/python2.7/site-packages")
from IPython.nbformat import v4
def strip_output(nb):
"""strip the outputs from a notebook object"""
for cell in nb.cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'execution_count' in cell:
cell['execution_count'] = u"0"
return nb
if __name__ == '__main__':
nb = v4.reads(sys.stdin.read())
nb = strip_output(nb)
sys.stdout.write(v4.writes(nb))