Skip to content

Instantly share code, notes, and snippets.

@PatrikHlobil
Created October 19, 2018 20:07
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 PatrikHlobil/9c633b597b2568d3f269c7d5c735974b to your computer and use it in GitHub Desktop.
Save PatrikHlobil/9c633b597b2568d3f269c7d5c735974b to your computer and use it in GitHub Desktop.
Delete Output of Jupyter Notebook file
import json
import os
#Define path of Jupyter notebook File:
notebook_path = r"c:\Users\Pat\Test.ipynb"
#Read in notebook as JSON and delete all output cells:
with open(notebook_path, "r") as f:
nb = json.load(f)
for i, cell in enumerate(nb["cells"]):
if "outputs" in nb["cells"][i]:
nb["cells"][i]["outputs"] = []
#Write new jupyter notebook file without output cells
output_path = os.path.join(os.path.splitext(notebook_path)[0] \
+ "_no_output.ipynb")
with open(output_path, "w") as f:
json.dump(nb, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment