forked from machielblok/analysis
/
git_ipynb_output_filter.py
64 lines (51 loc) · 1.38 KB
/
git_ipynb_output_filter.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/python
import sys
try:
runarg_idx = sys.argv.index('--rundir')
rundir = sys.argv[runarg_idx+1]
import os
os.chdir(os.path.expanduser(rundir))
except ValueError:
pass
version = None
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding('utf8')
try:
# Jupyter
from jupyter_nbformat import reads, write
except ImportError:
try:
# New IPython
from nbformat import reads, write
except ImportError:
# Old IPython
from IPython.nbformat.current import reads, write
version = 'json'
to_parse = sys.stdin.read()
if not version:
import json
json_in = json.loads(to_parse)
version = json_in['nbformat']
json_in = reads(to_parse, version)
if hasattr(json_in, 'worksheets'):
# IPython
sheets = json_in.worksheets
else:
# Jupyter
sheets = [json_in]
for sheet in sheets:
for cell in sheet.cells:
if "outputs" in cell:
cell.outputs = []
for field in ("prompt_number", "execution_number"):
if field in cell:
del cell[field]
for field in ("execution_count",):
if field in cell:
cell[field] = None
if hasattr(sheet.metadata, "widgets"):
del sheet.metadata["widgets"]
if 'signature' in json_in.metadata:
json_in.metadata['signature'] = ""
write(json_in, sys.stdout, version)