p2j: Convert Python scripts to Jupyter notebook with minimal intervention
Convert your Python source code to Jupyter notebook with zero intervention.
The purpose of this package is to be able to run a code on Jupyter notebook without having to copy each paragraph of the code into every cell. It's also useful if we want to run our code in Google Colab.
In a nutshell, every paragraph of your code is transformed into a code cell.
This parser isn't perfect, but you would be satisfactorily pleased with what you get.
pip install p2j
and you will get a
code_to_parse.ipynb Jupyter notebook. See
p2j -h for other arguments.
To run examples from this repository, first clone this repo
git clone https://github.com/raibosome/python2jupyter.git
and after you
cd into the project, run
examples/example.py is a Keras tutorial on building an autoencoder for the MNIST dataset, found here.
Tested on macOS 10.14 with Python 3.6.
How it works
Jupyter notebooks are just JSON files. The
py2nb.py reads the source code line-by-line and determines whether it should be a markdown cell or a code cell, using a rule-based method. It also respects the following:
- Blocks of indented code. Comments from within are kept as a code cell. Eg. classes, function definitions and loops
- Pylint directives are converted to code cells
├── p2j The parser module
│ ├── __init__.py
│ ├── examples Example codes that you can parse
│ ├── p2j.py Main file
│ └── templates JSON files needed to build the notebook
├── README.md This file
├── LICENSE Licensing
├── MANIFEST.in Python packaging-related
├── build Python packaging-related
├── dist Python packaging-related
├── p2j.egg-info Python packaging-related
└── setup.py Python packaging-related
There is no specific format that you should follow, but generally the parser assumes a format where your code is paragraphed. Each paragraph has the comments part and/or the code part. The comments will be automatically converted to a markdown cell while the code will be, you guessed it, the code cell.
Some examples of well-documented code (and from which you can test!):
Pull requests are very much encouraged!
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.