-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_notebook
More file actions
executable file
·56 lines (42 loc) · 1.38 KB
/
clear_notebook
File metadata and controls
executable file
·56 lines (42 loc) · 1.38 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
simple script for clearing all cells of an ipython notebook
Usage: `clear_notebook fname_in.ipynb [fname_out.ipynb]`
"""
import sys
from IPython.nbformat.current import reads, writes
def clear_outputs(fname_in, fname_out=None, clear_prompt_numbers=True):
"""
clears the output of all cells in an ipython notebook
Parameters:
===========
fname_in: string
filename of the incoming notebook
fname_out: string, optional
filename of the outgoing notebook
(default: fname_in)
clear_promt_numbers: bool, optional
clears the prompt numbers of all cells
(default: True)
"""
if fname_out is None:
fname_out = fname_in
with open(fname_in) as nb_file:
notebook = reads(nb_file.read(), 'json')
for wsheet in notebook.worksheets:
for cell in wsheet.cells:
if cell.get('cell_type', None) == 'code':
cell.outputs = []
if clear_prompt_numbers is True:
cell.pop('prompt_number', None)
with open(fname_out, 'w') as out:
out.write(writes(notebook, 'ipynb'))
if __name__ == '__main__':
FNAME_IN = sys.argv[1]
if len(sys.argv) > 2:
FNAME_OUT = sys.argv[2]
else:
FNAME_OUT = None
print "clearing {}".format(FNAME_IN)
clear_outputs(FNAME_IN, FNAME_OUT)