Adding or editing tutorials as Jupyter notebooks#

Under the doc/source/ folder of the SciPy tree you can find a few documents written in MyST Markdown format. The content in these files is executed when the SciPy documentation is built (locally or on CI) and any outputs generated by the execution are rendered in the final HTML files. In addition, they can be made interactive in the SciPy documentation website by using the jupyterlite-sphinx extension.

If you have a document written in Jupyter notebook format (an .ipynb file) and would like to submit it as part of the SciPy documentation, there are two options: you can convert it into a MyST Markdown file, and work with a .md file only, or you can pair your .ipynb file with a .md file and work with both. Note that .ipynb files should not be submitted to the SciPy documentation.

In this document, we address conversion to a MyST Markdown file. For more information about MyST-NB, Jupytext and pairing notebooks, you can also consult the Pairing tutorial on NumPy Tutorials. For even more information, please consult the MyST-NB documentation.

How to convert a .ipynb file to a .md file#

If you don’t need to keep the .ipynb file, and want to work with MyST Markdown only, follow the steps below.

  1. Install the jupytext tool, using pip install jupytext or conda install jupytext -c conda-forge;

  2. On your terminal, run jupytext notebook.ipynb --to myst, where notebook.ipynb should be replaced with the file you want to convert.

Now, the resulting .md file (in MyST Markdown format) should contain a preamble similar to the one below, indicating that its contents will be executed when the SciPy documentation is built:

---
jupytext:
   text_representation:
      extension: .md
      format_name: myst
      format_version: 0.13
      jupytext_version: 1.14.0
kernelspec:
   display_name: Python 3 (ipykernel)
   language: python
   name: python3
---

You don’t need to edit this preamble, as it is autogenerated.

Making the notebook interactive#

If you want to enable interactivity for your notebook when it is rendered in the SciPy documentation website, you need to add the following snippet at the beginning of the markdown file:

```{eval-rst}
.. notebooklite:: <filename>.md
   :new_tab: True
```

where <filename>.md should be replaced with the name of the file you are working with. This will create a button that allows users to open the notebook in a new tab and interact with it. See Bartlett’s test for equal variances (and its source) for an example.

Note

For simplicity, we don’t want certain notebook cells to be visible in the rendered documentation. We can hide them by adding "jupyterlite_sphinx_strip" to the tags section of the corresponding cell metadata. To do that in the Markdown file, add the following snippet right before the cell you want to hide:

+++ {"tags": ["jupyterlite_sphinx_strip"]}

Note that the +++ notation should come in pairs, surrounding the cell you want to hide.

Visit the NotebookLite directive documentation for more details and options.

Opening MyST Markdown files in the Jupyter Notebook application#

If you have the jupytext tool installed, you can open MyST Markdown .md files in the Jupyter Notebook application and execute them, just as you would with a .ipynb file.

Converting .rst files to MyST Markdown#

To convert a reStructuredText (.rst) file to MyST Markdown (.md), you can follow the formatting instructions above. You can also use tools such as RST-to-MyST to automate the conversion, but please note some review may be necessary to ensure the conversion is correct. In particular, some reStructuredText directives may need to be wrapped in the {eval-rst} MyST directive, as follows:

Some Markdown here

```{eval-rst}
.. some_rest_directive_here::
  :some_option: some_value
```

Some more Markdown here