py2jn is a utility for converting Python scripts into Jupyter Notebooks. Module-level multiline (triple quote) string literals are converted into markdown cells, and sections of code separated by such strings are converted into distinct code cells.
This package is a fork of sklam/py2nb, which is no longer maintained. Of the numerous forks of this original project, the only ones that appear to include significant additional development are:
- blueogive/py2nb Adds proper packaging including tests, and syntax for specifying raw notebook cells.
- MarcusJones/py2jnb Adds proper packaging including tests, and syntax for specifying notebook code cells. This package is available via pip.
There are also some independent packages with the same name and roughly the same goals:
- bjornaa/py2nb Requires cell boundaries to be indicated via comments.
- chicham/py2nb Requires markdown, raw, and code cells to be indicated via comments.
- williamjameshandley/py2nb Requires markdown cells to be indicated via comments. This package is available via pip.
The only requirement is nbformat. Under Ubuntu Linux 18.04, this requirement can be installed by the command
sudo apt-get install python-nbformat
or
sudo apt-get install python3-nbformat
for Python 2 or 3 respectively. It can also be installed via pip
, e.g.
sudo -H pip install nbformat
From the package root directory do
python setup.py install
To convert a Python script into a Jupyter notebook do
python -m py2jn input.py output.ipynb
To execute the notebook do
jupyter nbconvert --to=notebook --execute output.ipynb
and to convert to HTML do
jupyter nbconvert --to=html output.ipynb
Module py2jn
includes the following utility functions:
Function | Description |
---|---|
py_string_to_notebook |
Convert Python script in string to notebook object |
py_file_to_notebook |
Convert Python script file to notebook object |
write_notebook |
Write notebook object to a file |
write_notebook_to_string |
Write notebook object to a string |
python_to_notebook |
Convert Python script file to notebook file |
See files example.py
and example.ipynb
in the tests
directory.
Bug reports can be submitted via the GitHub Issues interface.
py2jn is distributed as open-source software under a BSD 3-Clause License (see the LICENSE
file for details).