{ "cells": [ { "cell_type": "markdown", "id": "a64b24be", "metadata": {}, "source": [ "# Pairing Jupyter notebooks and MyST-NB\n", "\n", "## What you'll do\n", "This guide will keep a Jupyter notebook synced _or paired_ between\n", "`.ipynb` and `.md`.\n", "\n", "## What you'll learn\n", "- The difference between Jupyter's json format and MyST-NB's markdown\n", " format\n", "- The benefits and drawbacks of json and markdown\n", "- How to keep `.ipynb` and `.md` files in sync\n", "\n", "## What you'll need\n", "- [Jupyter](https://jupyter.org/)\n", "- [Jupytext](https://jupytext.readthedocs.io/en/latest/index.html)\n", "\n", "---\n", "## Background\n", "\n", "The [NumPy tutorials](https://github.com/numpy/numpy-tutorials) are\n", "reviewed and executed as [MyST-NB](https://myst-nb.readthedocs.io/)\n", "notebooks. Content is easier to review in this markdown format. You can\n", "keep your `.ipynb` in sync with the content on NumPy tutorials. The\n", "NumPy tutorials use\n", "[Jupytext](https://jupytext.readthedocs.io/en/latest/index.html) to\n", "convert your `.ipynb` file to [MyST\n", "Markdown](https://github.com/mwouts/jupytext/blob/master/docs/formats.md#myst-markdown)\n", "format.\n", "\n", "Jupyter notebooks are stored on your disk in a\n", "[json format](https://nbformat.readthedocs.io/en/latest/format_description.html). The json format is\n", "very powerful and allows you to store almost any input and output that\n", "Python libraries can create. The drawback is that it is hard to see and compare changes made in the notebook file when reviewing pull requests, because this means the reviewers are looking only at the raw json files.\n", "\n", "MyST-NB notebooks are stored on your disk in a\n", "[markdown](https://en.wikipedia.org/wiki/Markdown) format. The markdown\n", "format is a lightweight markup language. Its key design goal is\n", "[_readability_](https://daringfireball.net/projects/markdown/syntax#philosophy).\n", "The drawback is that markdown can only store the inputs of your code.\n", "Each time you open the notebook, you must execute the inputs to see the\n", "output.\n", "\n", "> __Note:__ You should use [common mark](https://commonmark.org)\n", "> markdown cells. Jupyter only renders common mark markdown, but MyST-NB\n", "> supports a variety of restructured text directives. These Sphinx\n", "> markdown directives will render when NumPy tutorials are built into a\n", "> static website, but they will show up as raw code when you open in\n", "> Jupyter locally or on [Binder](https://mybinder.org).\n", "\n", "Consider these two versions of the same __Simple notebook example__. You\n", "have three things in the notebooks:\n", "\n", "1. A markdown cell that explains the code\n", " ```This code calculates 2+2 and prints the output.```\n", "2. A code cell that shows the code\n", " ```python\n", " x = 2 + 2\n", " print('x = ', x)\n", " ```\n", "3. The output of the code cell\n", " ```python\n", "\tx = 4\n", "\t```\n", "---\n", "__
Simple notebook example
__\n", "This code calculates 2+2 and prints the output." ] }, { "cell_type": "code", "execution_count": 1, "id": "9824cbbc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 4\n" ] } ], "source": [ "x = 2 + 2\n", "print(\"x = \", x)" ] }, { "cell_type": "markdown", "id": "ba0fb3ac", "metadata": {}, "source": [ "---\n", "\n", "Here are the two Simple notebook example raw inputs side-by-side:\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
json .ipynbMyST-NB .md
\n", "\n", "```json\n", "{\n", " \"cells\": [\n", " {\n", " \"cell_type\": \"markdown\",\n", " \"metadata\": {},\n", " \"source\": [\n", " \"This code calculates 2+2 and prints the output\"\n", " ]\n", " },\n", " {\n", " \"cell_type\": \"code\",\n", " \"execution_count\": 1,\n", " \"metadata\": {},\n", " \"outputs\": [\n", " {\n", " \"name\": \"stdout\",\n", " \"output_type\": \"stream\",\n", " \"text\": [\n", " \"x = 4\\n\"\n", " ]\n", " }\n", " ],\n", " \"source\": [\n", " \"x = 2 + 2\\n\",\n", " \"print('x = ', x)\"\n", " ]\n", " }\n", " ],\n", " \"metadata\": {\n", " \"kernelspec\": {\n", " \"display_name\": \"Python 3\",\n", " \"language\": \"python\",\n", " \"name\": \"python3\"\n", " },\n", " \"language_info\": {\n", " \"codemirror_mode\": {\n", " \"name\": \"ipython\",\n", " \"version\": 3\n", " },\n", " \"file_extension\": \".py\",\n", " \"mimetype\": \"text/x-python\",\n", " \"name\": \"python\",\n", " \"nbconvert_exporter\": \"python\",\n", " \"pygments_lexer\": \"ipython3\",\n", " \"version\": \"3.8.3\"\n", " }\n", " },\n", " \"nbformat\": 4,\n", " \"nbformat_minor\": 4\n", "}\n", "```\n", "\n", "\n", "\n", "````\n", "---\n", "jupytext:\n", " formats: ipynb,md:myst\n", " text_representation:\n", " extension: .md\n", " format_name: myst\n", " format_version: 0.12\n", " jupytext_version: 1.6.0\n", "kernelspec:\n", " display_name: Python 3\n", " language: python\n", " name: python3\n", "---\n", "\n", "This code calculates 2+2 and prints the output\n", "\n", "```{code-cell} ipython3\n", "x = 2 + 2\n", "print('x = ', x)\n", "```\n", "````\n", "
\n", "\n", "The MyST-NB `.md` is much shorter, but it does not save the output `4`.\n", "\n", "\n", "## Pair your notebook files `.ipynb` and `.md`\n", "\n", "When you submit a Jupyter notebook to NumPy tutorials, we (the reviewers) will convert\n", "it to a MyST-NB format. You can also submit the MyST-NB `.md` in your\n", "pull request.\n", "To keep the `.ipynb` and `.md` in sync--_or paired_--you need\n", "[Jupytext](https://jupytext.readthedocs.io/en/latest/index.html).\n", "\n", "Install `jupytext` using:\n", "\n", "```\n", "pip install jupytext\n", "```\n", "or\n", "```\n", "conda install jupytext -c conda-forge\n", "```\n", "\n", "Once installed, start your `jupyter lab` or `jupyter notebook`\n", "session in the browser. When launching `jupyter lab` it will ask you to rebuild\n", "to include the Jupytext extension.\n", "\n", "You can pair the two formats in the classic Jupyter, Jupyter Lab,\n", "or the command line:\n", "\n", "```{admonition} **1. Classic Jupyter Jupytext pairing**\n", ":class: toggle\n", "\n", "![Animation showing pairing with Jupyter classic](_static/01-classic.gif)\n", "```\n", "\n", "```{admonition} **2. JupyterLab Jupytext pairing**\n", ":class: toggle\n", "\n", "![Animation showing pairing with JupyterLab](_static/02-jupyterlab.gif)\n", "```\n", "\n", "````{admonition} **3. Command line Jupytext pairing**\n", ":class: toggle\n", "\n", "```sh\n", "jupytext --set-formats ipynb,myst notebook.ipynb\n", "```\n", "\n", "Then, update either the MyST markdown or notebook file:\n", "\n", "```sh\n", "jupytext --sync notebook.ipynb\n", "```\n", "````\n", "\n", "> __Note:__ With Jupytext installed, the classic Jupyter interface will\n", "> automatically open MyST files as notebooks. In JupyterLab, you can\n", "> right-click and choose \"Open With -> Notebook\" to open as a notebook.\n", "> The outputs of your code cells are only saved in the `.ipynb` file.\n", "\n", "## Wrapping up\n", "\n", "In this tutorial, you saw the json `.ipynb` and MyST-NB `.md` raw code\n", "to create Jupyter notebooks. You can use both formats to create\n", "tutorials. Now you can work in either a simple text editor like VIM\n", "or emacs or continue building notebooks in your browser. Jupytext can\n", "handle pairing to keep your work in sync." ] } ], "metadata": { "jupytext": { "formats": "ipynb,md:myst", "text_representation": { "extension": ".md", "format_name": "myst", "format_version": 0.13, "jupytext_version": "1.11.1" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" }, "source_map": [ 13, 82, 85 ] }, "nbformat": 4, "nbformat_minor": 5 }