Skip to content

Instantly share code, notes, and snippets.

@mratkovic
Last active January 8, 2017 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mratkovic/3e72de2d0fe464f11f9cb8292674e406 to your computer and use it in GitHub Desktop.
Save mratkovic/3e72de2d0fe464f11f9cb8292674e406 to your computer and use it in GitHub Desktop.
Python script that clears all output cells from given notebook
import argparse
from nbformat import read, write, NO_CONVERT
def main():
parser = argparse.ArgumentParser()
parser.add_argument("notebook", help="path to jupyter notebook")
parser.add_argument("-o", "--output", default="processed.ipynb", help="output path")
args = parser.parse_args()
src = args.notebook
output = args.output
print("Loading notebook...")
with open(src, 'r') as f:
nb = read(f, NO_CONVERT)
print("Removing all output...")
out_cells = filter(lambda c: c.cell_type == 'code', nb.cells)
for cell in out_cells:
cell.outputs = []
print("Saving to %s..." % output)
with open(output, 'w', encoding='utf8') as f:
write(nb, f, NO_CONVERT)
print("Completed...")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment