Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Remove output from iPython notebook files.
from IPython.nbformat import current
import io
from os import remove, rename
from shutil import copyfile
from subprocess import Popen
from sys import argv
for filename in argv[1:]:
# Backup the current file
backup_filename = filename + ".backup"
# Read in the notebook
with,'r',encoding='utf-8') as f:
notebook = current.reads(,format="ipynb")
# Strip out all of the output and prompt_number sections
for worksheet in notebook["worksheets"]:
print worksheet
for cell in worksheet["cells"]:
cell.outputs = []
if "prompt_number" in cell:
del cell["prompt_number"]
# Write the stripped file
with, 'w', encoding='utf-8') as f:
# Run git add to stage the non-output changes
print("git add",filename)
# Restore the original file; remove is needed in case
# we are running in windows.
# remove(filename)
# rename(backup_filename,filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.