-
-
Save TrishGillett/66727bdfc834c6532362 to your computer and use it in GitHub Desktop.
When an ipython notebook is minorly corrupt or otherwise unopenable and you've exhausted other recovery options, you can try running this function to print out all of the code and headings in such a way that you could recreate the notebook by copy-pasting them into a fresh notebook. It's not super fun, but better than nothing in some cases.
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 show_nb_inputs.py notebook.ipynb | |
""" | |
import sys | |
import io | |
from IPython.nbformat import current | |
def show_notebook_inputs(nb): | |
"""print text needed to recreate a notebook""" | |
k = 1 | |
for ws in nb.worksheets: | |
for cell in ws.cells: | |
if cell.cell_type == 'code': | |
print "#------------code block {0}------------\n".format(k) | |
print cell.input | |
print "\n\n" | |
k += 1 | |
elif cell.cell_type == 'heading': | |
print "#------------heading type {0}------------\n".format(cell.level) | |
print "# " + cell.source | |
print "\n\n" | |
if __name__ == '__main__': | |
fname = sys.argv[1] | |
with io.open(fname, 'r') as f: | |
nb = current.read(f, 'json') | |
show_nb_inputs(nb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment