-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnbstripout
executable file
·30 lines (26 loc) · 1.08 KB
/
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
#!/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, codecs
#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")
import nbformat as fmt
def strip_output(nb):
"""strip the outputs from a notebook object"""
for cell in nb.cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'prompt_number' in cell:
cell['prompt_number'] = None
return nb
if __name__ == '__main__':
nb = fmt.read(sys.stdin, fmt.NO_CONVERT)
nb = strip_output(nb)
if sys.version_info < (3, 0):
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
fmt.write(nb, sys.stdout, fmt.NO_CONVERT)