File: nbstripout

package info (click to toggle)
seaborn 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,896 kB
  • ctags: 1,183
  • sloc: python: 8,372; makefile: 181
file content (31 lines) | stat: -rwxr-xr-x 938 bytes parent folder | download | duplicates (2)
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
#!/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.
"""
 
import io
import sys
 
from IPython.nbformat import current
 
def strip_output(nb):
    """strip the outputs from a notebook object"""
    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')