Converts a .py python source file to a V.4 .ipynb jupyter/ipython notebook.
Based on this and this posts. Question originally asked on stackoverflow.
(Forked from py2ipynb)
This fork aims to allow back in forth between an ipynb jupyter/ipython notebook and the exported .py file. We want to allow using source control and revision comments on the .py file and import back into a notebook as a deployment step.
Adaptations
- Using pycharm style since it seems to correspond to the notebook export style
- Make Python 3 friendly
This script has been tested with Python 3.5.2. The versions of dependencies specified in the requirements.txt are based on the versions needed by jupyter 1.0.0. If you upgrade your jupyter installation, you might have to adjusts the requirements accordingly.
$ python3 py2ipynb.py from-notebook.py to-notebook.ipynb
Unfortunately, the out-of-the-box Jupyter converter to .py does not deliminate Markdown cells, it just puts the Markdown in comments. Doing so, we don't know when a code cell ends and a Markdown cell begins. If we do want to restore Markdown properly, this suggests we should develop a custom exporter. The project is nbconvert. The current Jinja template file is python.tpl. The template and/or code must be modified to suit the Markdown cell marker like so:
##
# md
"""
# Header
* item 1
* item 2
"""