From bdb4b6305d4ee4fbe907cf200c2651169e6f9a40 Mon Sep 17 00:00:00 2001 From: Edward Delaporte Date: Wed, 14 Feb 2024 13:18:29 -0600 Subject: [PATCH] Recipe for cleaning data cells out of Jupyter notebooks --- files/gitattributes | 1 + util/jupyter_git_clean.py | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 files/gitattributes create mode 100644 util/jupyter_git_clean.py diff --git a/files/gitattributes b/files/gitattributes new file mode 100644 index 0000000..2d71c45 --- /dev/null +++ b/files/gitattributes @@ -0,0 +1 @@ +*.ipynb filter=~/dotfiles/util/jupyter_git_clean.py \ No newline at end of file diff --git a/util/jupyter_git_clean.py b/util/jupyter_git_clean.py new file mode 100644 index 0000000..301ea78 --- /dev/null +++ b/util/jupyter_git_clean.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +""" +Clear outputs of IPython notebooks. + +By default, it prints the notebooks without outputs into stdout. +When the --in-place option is given, all files will be overwritten. + +From +https://github.com/ivanov/nbutils/blob/master/nbstrip.py + +""" + +import sys + +from IPython.nbformat import current as nbformat + + + + +def clear_outputs(nb): + """Clear output of notebook `nb` INPLACE.""" + for ws in nb.worksheets: + for cell in ws.cells: + cell.outputs = [] + + +def stripoutput(inputs, inplace=False): + """ + Strip output of notebooks. + + Parameters + ---------- + inputs : list of string + Path to the notebooks to be processed. + inplace : bool + If this is `True`, outputs in the input files will be deleted. + Default is `False`. + + """ + for inpath in inputs: + with file(inpath) as fp: + nb = nbformat.read(fp, 'ipynb') + clear_outputs(nb) + if inplace: + with file(inpath, 'w') as fp: + nbformat.write(nb, fp, 'ipynb') + else: + nbformat.write(nb, sys.stdout, 'ipynb') + + +def main(): + from argparse import ArgumentParser + parser = ArgumentParser(description=__doc__) + parser.add_argument('inputs', nargs='+', metavar='input', + help='Paths to notebook files.') + #XXX: TODO: handle alternative outputs here, such as to stdout or to file + parser.add_argument('-i', '--inplace', '--in-place', default=True, + action='store_true', + help='Overwrite existing notebook when given.') + + args = parser.parse_args() + stripoutput(**vars(args)) + + +if __name__ == '__main__': + main() -- 2.47.3