Created
April 3, 2013 22:05
-
-
Save damianavila/5305869 to your computer and use it in GitHub Desktop.
Remove output from IPython notebook from the command line (dev version 1.0)
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
""" | |
Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ] | |
Modified from remove_output by Minrk | |
""" | |
import sys | |
import io | |
import os | |
from IPython.nbformat.current import read, write | |
def remove_outputs(nb): | |
"""remove the outputs from a notebook""" | |
for ws in nb.worksheets: | |
for cell in ws.cells: | |
if cell.cell_type == 'code': | |
cell.outputs = [] | |
if __name__ == '__main__': | |
fname = sys.argv[1] | |
with io.open(fname, 'r') as f: | |
nb = read(f, 'json') | |
remove_outputs(nb) | |
base, ext = os.path.splitext(fname) | |
new_ipynb = "%s_removed%s" % (base, ext) | |
with io.open(new_ipynb, 'w', encoding='utf8') as f: | |
write(nb, f, 'json') | |
print "wrote %s" % new_ipynb |
this worked for me a few months back, but now I get the following error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1843494: character maps to
error is thrown for nb =read(f, 'json')
I love it..
thank you dude
I wasn't able to get this version or @mathematicalmichael version to work due to errors.
I was able to recover my 75 MB .ipynb file that was too large to load in Jupyter notebook on Chrome. I made the mistake of printing a gigantic list to the notebook output and saving the .ipynb. The solution was to open the .ipynb file with a simple text editor and delete the gigantic list output and this dropped the file size to 3 MB. Now it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey!
Thanks for this, it put me on the right track. In my case, history was being clogged up with 10's to 100's of MBs for a file that is supposed to be under 50KB. I removed history and all cell outputs and found the file to be back to its correct size.
The nuclear option....
Here's my update for Python 3:
https://gist.github.com/mathematicalmichael/a206b2a21de0bf88a5703e8700403019