Marc Wouts
Jupyter Notebooks are great!
But .ipynb
files have a few limitations:
(1) Well, this is changing, see Jupyter Notebooks in the IDE: VS Code versus PyCharm
(2) Too large, mix user inputs with programmatic outputs
(3) JSON is hard to merge
The idea behind Jupytext
Formats supported by Jupytext:
pip install jupytext
or conda install jupytext -c conda-forge
Restart Jupyter... and text files become notebooks!
Before | After | |
---|---|---|
Demo
example.py
has a notebook icon! Open it as a notebook..py
file with the Include Metadata command(1) If your edit takes more than 2 minutes, answer 'Reload' to the message in Jupyter telling you that the notebook has changed on disk, and consider a) turning the Jupyter autosave off or b) closing the notebook while you edit the text file
(2) To be solved at the next slide
Text notebooks with persistent outputs!
The notebook is saved to an .ipynb
and a .py
file.
Outputs are loaded from the .ipynb
file, and inputs are
taken from the .py
file if present.
Paired notebooks are robust. You can delete any file in the pair.
Share only one file, the other one will be recreated.
Jupyter Lab | Jupyter Notebook |
---|---|
Create a jupytext.toml
file with this content:
default_jupytext_formats = ipynb,py:percent
You can also pair in different subfolders:
default_jupytext_formats = notebooks///ipynb,scripts///py:percent
jupytext --to markdown notebook.ipynb
jupytext --to script notebook.ipynb
jupytext --to notebook notebook.py
jupytext --to notebook notebook.md
jupytext --to notebook --update notebook.py
jupytext --to notebook --update notebook.md
Jupytext formats are concise and have clean diffs
.py
and .md
files are easy to merge - unlike .ipynb
(JSON)
Easily find all the references to a function or a dataset
within your notebooks!
Access to your notebooks as scripts, and refactor them:
You like to apply flake8
, black
, isort
on scripts?
Jupytext let you do the same on notebooks:
jupytext --pipe black notebook.ipynb
Because you can do different things with them!
Notebook content | You want? | Recommended format | Compatible with |
---|---|---|---|
Mostly code | A simple script | py:light |
Everything |
A notebook-like script | py:percent |
VS Code, PyCharm Pro, Spyder, Hydrogen, PTVS | |
Text & code | Render the .md on GitHub |
md:markdown |
VS Code, PyCharm, GitHub, ... |
Write a book (cross references, biblio) | md:myst |
Jupyter Book | |
Edit a notebook in Jupyter & RStudio | Rmd |
RStudio, knitr , bookdown |
Write a book made of notebooks
Plotly.py's documentation is a collection of Jupytext Markdown Notebooks.
Read or edit them directly on GitHub!
Lint notebooks with isort
and black
in a pre-commit hook