IPython and Jupyter Notebooks: Automatically Export .py and .html

[Updated 2016-03-04 to support Jupyter 4 notebooks – see below.]

IPython notebooks are stored in a format that is not particularly human-readable and doesn’t work well in version control.

One way to solve this problem is to automatically export the code from IPython notebooks into a vanilla Python file after each save.

It’s also useful to automatically generate a HTML file of the notebook on each save. This can be done manually in Jupyter (File > Download as > HTML), but if you always want this, doing it automatically is much easier.

Use the following code to automatically save a .py and a .html file when you save a notebook in Jupyter. These two files will be saved in the same folder as the parent .ipynb file.

First, run ipython locate profile default, which will give you the path to save the following code in.

Save the code below in this folder as ipython_notebook_config.py:

Now, run ipython notebook. You will see an error message in the terminal if there are any syntax or runtime errors with ipython_notebook_config.py. If everything looks good, go to your web browser, open a notebook, and click the Save/Checkpoint button (it’s the floppy disk icon in the Jupyter toolbar). You should see a .py and a .html file appear alongside your .ipynb file.

Update for Jupyter 4 notebooks

After the big split between IPython and Jupyter, and the accompanying update to Jupyter 4.x, the config file above no longer works. Instead, put the following in a file saved at ~/.jupyter/jupyter_notebook_config.py to achieve the same thing:

This code could be DRYer, but it does work.


🌟 Was this page helpful? Please let me know with this quick, 3 question survey.

19 responses
How do you turn this off? I deleted the config file, but it is still generating.
@Marie – If you have deleted the correct config file and restarted IPython/Jupyter then as far as I know it should be turned off. However, I'm not an expert with how these these config files work with IPython/Jupyter, so I may be missing something here. If you find a solution, please post a comment!
Thanks, saves me a ton of headache!
Great - I was looking for something like this. But I don't get how it helps with the version control problem as far as *notebooks* are concerned. Won't this just give us version control over what's contained in the code cells - but not the Markdown, nor the cell structure?
Ah wait - sorry I posted my question so soon, because I just found these scripts. One of them does the export in such a way as to preserve enough metadata to allow another to reconstruct a complete notebook file (minus the output cells). I guess these could be integrated with your script? https://www.balabit.com/blog/how-to-version-con...
Thank You So Very Much !! Perfect for git commits. Huge Time Saver - Removes a former hassle. :-) Tweeted @sherylhohman This should be a built in option !!
Thanks for sharing this information
12 visitors upvoted this post.