Created
July 15, 2014 08:45
-
-
Save amitu/2d9674f8326a1286d85d to your computer and use it in GitHub Desktop.
Remove output from iPython notebook files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
copyfile(filename,backup_filename) | |
try: | |
# Read in the notebook | |
with io.open(filename,'r',encoding='utf-8') as f: | |
notebook = current.reads(f.read(),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 io.open(filename, 'w', encoding='utf-8') as f: | |
current.write(notebook,f,format='ipynb') | |
# Run git add to stage the non-output changes | |
print("git add",filename) | |
#Popen(["git","add",filename]).wait() | |
finally: | |
# Restore the original file; remove is needed in case | |
# we are running in windows. | |
# remove(filename) | |
# rename(backup_filename,filename) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment