/
clear_notebook_output.py
39 lines (28 loc) · 1.1 KB
/
clear_notebook_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
#! /usr/bin/env python
""" Simple script to strip a notebook of all cell output.
Modified from https://gist.github.com/damianavila/5305869
"""
import sys
import argparse
import nbformat
_DEFAULT_VERSION = 4
def strip_output(nb):
for cell in nb['cells']:
if cell['cell_type'] == 'code':
cell.outputs = []
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('paths', action='store', nargs='+', help="Path to the IPython notebook to be stripped")
parser.add_argument('-i', '--inplace', action='store_true',
help="Modify notebooks in-place instead of writing to stdout")
parser.add_argument('-v', '--version', action='store', type=int, default=_DEFAULT_VERSION,
help="IPython notebook version to parse as (default: {}).".format(_DEFAULT_VERSION))
args = parser.parse_args()
for path in args.paths:
with open(path, 'r') as f:
nb = nbformat.read(f, args.version)
strip_output(nb)
if args.inplace:
with open(path, 'w') as f:
nbformat.write(nb, f)
else:
nbformat.write(nb, sys.stdout)