-
-
Save mathematicalmichael/a206b2a21de0bf88a5703e8700403019 to your computer and use it in GitHub Desktop.
| """ | |
| Usage: python remove_output.py notebook.ipynb | |
| Modified from remove_output by Minrk | |
| Modified from remove_output by mathematicalmichael | |
| """ | |
| import sys | |
| import io | |
| import os | |
| from nbformat import read, write, NO_CONVERT | |
| def remove_outputs(nb): | |
| """remove the outputs from a notebook""" | |
| cell_rm = [] | |
| for cell in nb.cells: | |
| cell.outputs = [] | |
| nb.metadata.history = [] | |
| if __name__ == '__main__': | |
| fname = sys.argv[1] | |
| with io.open(fname, 'r') as f: | |
| nb = read(f, NO_CONVERT) | |
| 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, NO_CONVERT) | |
| print("wrote %s"% new_ipynb) |
Thanks a lot! <3
so glad this could be of use to someone. I lost all too much time to bloated notebooks before I got IT to disable an extension that was causing it. Is this starting to surface on google?
Actually nope, I found it by your comment on the stackoverflow (maybe) :)
Please add a ``if 'outputs' in cell' between line 15 and 16. Otherwise, for markdown cells, it will raise a message when opening the notebook with jupyter.
THanks for sharing this solution. Unfortunately it did not work for me, here is the error:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 467852: character maps to
I was able to recover my 75 MB .ipynb file though. I made the mistake of printing a gigantic list to the notebook output. I was able 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.
@emskiphoto seems like the problem is you've got some unhandled character. this snippet seems to work in general but yeah, in no way does it have robust error-handling =/
sorry it didn't work for you, but it sounds like you did the equivalent thing manually. automation like this is really only useful when the problem is persistent. I for one, haven't used it since publication because the underlying problem bloating my notebooks was fixed.
Fantastic thanks guys!