Project: Escape from Jupyter

All data in Jupyter notebooks is saved in JSON encoded text files with the .ipynb extension. This storage format makes it easy to retrieve notebook data without using Jupyter notebook software. This could come handy e.g. if the Jupyter project were to be discontinued at some point in the future, and we would like to recover content of old notebook files. The goal of this exercise is to implement such data recovery process.


Write a function nb2py which takes as an argument a Jupyter notebook file, and creates a text file with the following properties.

  • The file created by the function should have the same name as the input file, but it should have the extension .py instead of .ipynb. For example, if the input file is mynotebook.ipynb, then the output file should be

  • The code from all code cells in the notebook should be saved without changes. The code from a cell number x should be preceded by a line with a Python comment of the form:

# In[x]:
  • Text from markdown cells should be saved as Python comments. i.e. every line of the text should be preceded by the # character.

  • Content of markdown and code cells should be saved in the same order in which it appears in the notebook file.

  • Output from code cells should be discarded.

  • For simplicity, assume that markdown cells contain text only (and do not have e.g. embedded images).


As an illustration of how the nb2py function should work, below are links to a sample Jupyter notebook file and a text file in the form that should be produced from this notebook by nb2py.