]>
apps.delaporte.us Git - dotfiles.git/blob - util/jupyter_git_clean.py
4 Clear outputs of IPython notebooks.
6 By default, it prints the notebooks without outputs into stdout.
7 When the --in-place option is given, all files will be overwritten.
10 https://github.com/ivanov/nbutils/blob/master/nbstrip.py
16 from IPython
.nbformat
import current
as nbformat
21 def clear_outputs(nb
):
22 """Clear output of notebook `nb` INPLACE."""
23 for ws
in nb
.worksheets
:
28 def stripoutput(inputs
, inplace
=False):
30 Strip output of notebooks.
34 inputs : list of string
35 Path to the notebooks to be processed.
37 If this is `True`, outputs in the input files will be deleted.
42 with file(inpath
) as fp
:
43 nb
= nbformat
.read(fp
, 'ipynb')
46 with file(inpath
, 'w') as fp
:
47 nbformat
.write(nb
, fp
, 'ipynb')
49 nbformat
.write(nb
, sys
.stdout
, 'ipynb')
53 from argparse
import ArgumentParser
54 parser
= ArgumentParser(description
=__doc__
)
55 parser
.add_argument('inputs', nargs
='+', metavar
='input',
56 help='Paths to notebook files.')
57 #XXX: TODO: handle alternative outputs here, such as to stdout or to file
58 parser
.add_argument('-i', '--inplace', '--in-place', default
=True,
60 help='Overwrite existing notebook when given.')
62 args
= parser
.parse_args()
63 stripoutput(**vars(args
))
66 if __name__
== '__main__':