Skip to main content

Parametrize and run scripts as notebooks with jupytext and papermill

Build Status codecov.io Language grade: Python Pypi pyversions Binder

This is on-going research on how to run scripts as notebooks using Jupytext and Papermill.

The corresponding GitHub issues are Jupytext #231 and Papermill #365.

Note that the below is a Bash Jupyter notebook that is tested on our CI. If you wish, you can also open and run it interactively on Binder.

cd demo

Jupytext and Papermill

It is possible to convert a script to a notebook using Jupytext, and then to run it using Papermill. Here we use --set-kernel - to use the kernel that matches the current Python environment.

jupytext script.py -o notebook.ipynb --set-kernel -
papermill notebook.ipynb executed_notebook.ipynb -p integer 2 -p text 'updated text'
python -c "import nbformat
with open('executed_notebook.ipynb') as fp:
    nb = nbformat.read(fp, as_version=4)
print(nb.cells[-1]['outputs'][0]['text'])"

Inject parameters in a script using Papermill

Papermill needs the language information in the kernel to inject the parameters in the notebooks, so in this case we also need to set a kernel for the notebook.

jupytext script.py -o notebook.ipynb --set-kernel -
papermill notebook.ipynb notebook_with_parameters.ipynb --prepare-only -p integer 3 -p text 'updated text, v3'

Once the parameters have been injected, we can convert back the notebook to a script, and drop the kernel information:

jupytext notebook_with_parameters.ipynb -o script_with_parameters.py --update-metadata '{"kernelspec":null, "jupytext":null}'
cat script_with_parameters.py

And finally, we run the script using the Python interpreter:

python script_with_parameters.py

Towards Papermill + Jupytext?

With the papermill_jupytext package, we can open Jupytext scripts with the txt:// address. A kernel pointing to the current Python environment is injected in the documents that have no kernel.

papermill txt://script.py executed_notebook.ipynb  -p integer 4 -p text 'updated text, v4'
python -c "import nbformat
with open('executed_notebook.ipynb') as fp:
    nb = nbformat.read(fp, as_version=4)
print(nb.cells[-1]['outputs'][0]['text'])"

Release files for papermill-jupytext 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for papermill-jupytext 0.0.1
File Size Uploaded
papermill_jupytext-0.0.1.tar.gz 3.7 kB Details

Release files / papermill_jupytext-0.0.1.tar.gz

Download URL papermill_jupytext-0.0.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e959c868498debd557316fd265483d104708255fd7d6c19708821b1e86b502bf
BLAKE2b-256 checksum
How to use checksums
e0a611a5c9193f66644724b8ad22f40230d2fff5097dc5bdf98516fd122a1ac0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page