Skip to content

Add myst-nb exercise#320

Open
frankier wants to merge 5 commits intomainfrom
add-myst-nb-exercise
Open

Add myst-nb exercise#320
frankier wants to merge 5 commits intomainfrom
add-myst-nb-exercise

Conversation

@frankier
Copy link
Collaborator

@frankier frankier commented Mar 9, 2026

This PR also adds some cleanups to the other Sphinx exercises


with this line so that Sphinx can parse Markdown files:
```python
extensions = ['myst_parser']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a strong opinion, but the content of conf.py created by sphinx-quickstart (8.2.3) use single quotes, so it's a little weird to change this here.

```

2. In the file `conf.py` modify "extensions" and add 3 lines:
2. In the file `conf.py` modify "extensions" and add the `autodoc2_packages` with `"multiply.py"`:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. In the file `conf.py` modify "extensions" and add the `autodoc2_packages` with `"multiply.py"`:
2. In the file `conf.py` add `autodoc2` to the "extensions", and add the list `autodoc2_packages` which will mention `"multiply.py"`:


`````{exercise} Sphinx-4: Writing Sphinx content with Jupyter

1. Create a text-based notebook files `flower.md` in the same directory as the `index.rst` file. This file will be converted to a Jupyter notebook by the `myst_nb` Sphinx extension and then executed by Jupyter. Fill the file with the following content:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Create a text-based notebook files `flower.md` in the same directory as the `index.rst` file. This file will be converted to a Jupyter notebook by the `myst_nb` Sphinx extension and then executed by Jupyter. Fill the file with the following content:
1. For simplicity, create a text-based notebook files `flower.md` in the same directory as the `index.rst` file. This file will be converted to a Jupyter notebook by the `myst_nb` Sphinx extension and then executed by Jupyter. Fill the file with the following content:

```
````

2. In the file `conf.py` modify `extensions` to remove `"myst_parser"` and add `"myst_nb"` (you will get an error if you include both):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. In the file `conf.py` modify `extensions` to remove `"myst_parser"` and add `"myst_nb"` (you will get an error if you include both):
2. In the file `conf.py` modify `extensions` to remove `"myst_parser"` and add `"myst_nb"` ([you will get an error if you include both](https://myst-nb.readthedocs.io/en/latest/quickstart.html)):

ax.plot(theta, np.full(len(theta), -1));
```
````

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note that there needs to be a title in the notebook (a heading starting with a single `#`), that will be used as an entry and link in the table of content.


4. Re-build the documentation and check the "Flower" section.

5. (Optional) As an alternative to including a text-based notebook with an `.md` file in your documentation, you can add a `.ipynb` files saved from Jupyter notebook or Jupyter lab. Just make sure to 1) start the notebook with a markdown cell with a `# Title` so it can be included in the table of contents 2) save the notebook in the same directory as `index.rst` and 3) list it in the toctree in `index.rst`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested it, there is no need to save the notebook in the same directory as index.rst, as long as the path is correctly specified.

Also, I think that jupyter notebooks as ipynb's may still be more appealing and direct than the text-based alternative (although, I love the text-based ones), and the fact that ipynb stores by default the results might be used, as discussed previously elsewhere, for validation.

So I would not present that as a secondary idea, but as an idea as relevant as the first one.

Suggested change
5. (Optional) As an alternative to including a text-based notebook with an `.md` file in your documentation, you can add a `.ipynb` files saved from Jupyter notebook or Jupyter lab. Just make sure to 1) start the notebook with a markdown cell with a `# Title` so it can be included in the table of contents 2) save the notebook in the same directory as `index.rst` and 3) list it in the toctree in `index.rst`.
5. You can add a `.ipynb` files saved from Jupyter notebook or Jupyter lab. Just make sure to list it in the toctree in `index.rst` with the correct path.
If you have problems, consider cleaning manually the `jupyter_execute` directory.

@mmesiti
Copy link
Collaborator

mmesiti commented Mar 9, 2026

Please consider my suggestions, but do not feel obliged to accept the ones that look mostly opinion-based.

One could add a couple of sentences about the advantages and disadvantages of using text-based notebook formats, but maybe this can become its own issue that we can postpone for the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants