Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
Have you always wished Jupyter notebooks were plain text documents? Wished you could edit them in your favorite IDE? And get clear and meaningful diffs when doing version control? Then... Jupytext may well be the tool you're looking for!
Common use cases for Jupytext are: - Doing version control on Jupyter Notebooks - Editing, merging or refactoring notebooks in your favorite text editor - Applying Q&A checks on notebooks.
You can install Jupytext with -
pip install jupytext- or
conda install jupytext -c conda-forge.
Please note that Jupytext includes an extension for Jupyter Lab. In the latest version of Jupytext, this extension is compatible with Jupyter Lab >= 3.0 only. If you use Jupyter Lab 2.x, please either stay with Jupytext 1.8.2, or install, on top of the latest pip or conda version of Jupytext, a version of the extension that is compatible with Jupyter Lab 2.x:
jupyter labextension install [email protected] # For Jupyter Lab 2.x
Then, restart your Jupyter server (for more installation details, see the install section in the documentation).
When Jupytext is installed,
.mdfiles have a notebook icon. And you can really open and run these files as notebooks
The most convenient way to use Jupytext is probably through paired notebooks.
To pair a given
.ipynbor text notebook to an additional notebook format, use either
jupytextat the command line
jupytext --set-formats ipynb,py:percent notebook.ipynb
see the documentation.
with e.g. the following content:
formats = "ipynb,py:percent"
see the documentation.
When you save a paired notebook in Jupyter, both the
.ipynbfile and the text version are updated on disk.
When a paired notebook is opened or reloaded in Jupyter, the input cells are loaded from the text file, and combined with the output cells from the
You can edit the text representation of the notebook in your favorite editor, and get the changes back in Jupyter by simply reloading the notebook (Ctrl+R in Jupyter Notebook, "reload notebook" in Jupyter Lab). And the changes are propagated to the
.ipynbfile when you save the notebook.
Alternatively, you can synchronise the two representations by running
jupytext --sync notebook.ipynbat the command line.
Jupytext implements many text formats for Jupyter Notebooks. If your notebook is mostly made of code, you will probably prefer to save it as a script: - Use the percent format, a format with explicit cell delimiters (
# %%), supported by many IDE (Spyder, Hydrogen, VS Code, PyCharm and PTVS) - Or use the light format, if you prefer to see fewer cell markers.
If your notebook contains more text than code, if you are writing a documentation or a book, you probably want to save your notebook as a Markdown document - Use the Jupytext Markdown format if you wish to render your notebook as a
.mdfile (without its outputs) on GitHub - Use the MyST Markdown format, a markdown flavor that “implements the best parts of reStructuredText”, if you wish to render your notebooks using Sphinx or Jupyter Book. - Use the R Markdown format if you want to open your Jupyter Notebooks in RStudio.