-
Notifications
You must be signed in to change notification settings - Fork 2
/
drop_ipynb_output.py
executable file
·69 lines (54 loc) · 2.16 KB
/
drop_ipynb_output.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
65
66
67
68
69
#!/usr/bin/env python
""" See README.md for instructions on how to install this script. """
# Standard library imports
import os
import sys
import json
import fnmatch
# Path to the ignore file
CLEAN_IPYNB_IGNORE_PATH = "~/.config/git/clean_ipynb_ignore"
CLEAN_IPYNB_IGNORE_PATH = os.path.expanduser(CLEAN_IPYNB_IGNORE_PATH)
def dumpit(json_in):
json.dump(json_in, sys.stdout, sort_keys=True, indent=1, separators=(",",": "))
# the git smudge filter will "cat" the notebook file contents and pipe in to this script
nb = sys.stdin.read()
json_in = json.loads(nb)
# we use the clean filter to pass the name of the file in to this script as a command-line argument
nb_filename = os.path.abspath(sys.argv[1])
# by default, suppress output and line numbers
suppress_output = True
if os.path.isfile(CLEAN_IPYNB_IGNORE_PATH): # if the clean_ipynb_ignore file exists
with open(os.path.expanduser(CLEAN_IPYNB_IGNORE_PATH), "r") as f:
for line in f.readlines():
line = line.strip()
if line: # make sure the line is not empty
line = os.path.abspath(os.path.expanduser(line))
if fnmatch.fnmatch(nb_filename, line):
# check if the nb filename matches any of the glob patterns
# or is in an ignored directory
suppress_output = False
break
# get the metadata block of the notebook
metadata = json_in.get("metadata", dict())
if "git" in metadata:
# 2nd bit in 'or' is for backwards compatibility
for name in ["clear_outputs", "drop_outputs", "suppress_outputs"]:
if name in metadata['git'] and not metadata['git'][name]:
suppress_output = False
# exit early and return the file as-is if we shouldn't filter output cells
if not suppress_output:
sys.stdout.write(nb)
sys.exit(0)
def clean(cell):
""" Remove the output from a cell and clear the execution count. """
if "outputs" in cell:
cell["outputs"] = []
if "execution_count" in cell:
cell["execution_count"] = None
try:
for cell in json_in["cells"]:
clean(cell)
except:
# probably an old-style notebook
pass
dumpit(json_in)