napari / docs

Documentation for napari (other than API docs)
BSD 3-Clause "New" or "Revised" License
11 stars 37 forks source link

Gallery scraper: enable preserving viewer between code cells. #527

Open psobolewskiPhD opened 2 weeks ago

psobolewskiPhD commented 2 weeks ago

🧰 Task

This issue stems from the discussion here: https://github.com/napari/napari/pull/7319#discussion_r1835770414 Essentially, the idea was to make a notebook example that involves multiple steps each using the same viewer. As it turns out, the scraper runs for each block, takes a screenshot, and then the viewer is cleaned up, so any following cells referring to it cause exceptions.

One can work around it by calling a new viewer in each cell, as done here: https://github.com/napari/napari/blob/main/examples/dynamic-projections-dask.py This looks good: https://napari.org/stable/gallery/dynamic-projections-dask.html#sphx-glr-gallery-dynamic-projections-dask-py But the actual notebook is awkward and not how a notebook would be used locally?

Ideally, the scraper would work the same way as jupytext/jupyter book parsing of notebooks in our workshop repo, e.g. https://napari.org/napari-workshop-template/notebooks/spot_detection.html

Here the viewer persists until closed and nbscreenshot can be used to show viewer states.

lucyleeow commented 1 week ago

Some details on how to implement single viewer for each example:

The current scraper looks for any QtMainWindow instances and takes a screenshot of all of them. AFAICT we can still do this if we persist the viewer between code blocks.

To clean up the viewer we can use the Sphinx-Gallery config reset_modules, which lets you add a function to run (before|after|before and after) each example.

Taking a look at the examples, the following use code block splitters, which may need to be amended if we change how the screenshot-er works: