Skip to content

DOC/DEV: add docs for enabling interactive examples#20843

Merged
rgommers merged 13 commits intoscipy:mainfrom
steppi:idocs
Jun 4, 2024
Merged

DOC/DEV: add docs for enabling interactive examples#20843
rgommers merged 13 commits intoscipy:mainfrom
steppi:idocs

Conversation

@steppi
Copy link
Copy Markdown
Member

@steppi steppi commented May 31, 2024

This PR adds information to the releasing section of the core developer guide and the "Rendering documentation locally with Sphinx" section of the contributor guide describing how to enable interactive documentation with jupyterlite-sphinx.

The information I've added to the releasing section is terse, giving only a mechanical description of what steps to take. This seems in line to me with how releasing.rst.inc is structured. I've added these steps to the "Wrapping Up" subsection.

The information I've given in the contributor guide has more detail, giving some explanation of what is going on, and showing what try_examples.json looks like in a clean doc build. I think that some contributors may be curious about these things.

Additional information

Pyodide 0.26 was released recently, which updated the SciPy version to 1.12. My understanding is that the jump from 1.11 to 1.12 was rather large, and examples running with 1.12 should work well enough for the latest version of SciPy (now 1.13, soon to be 1.14). I think now is a good time for us to turn on the interactive examples. I suppose this should go through discourse.

cc @rgommers, @melissawm

@github-actions github-actions Bot added Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org DX Everything related to making the experience of working on SciPy more pleasant labels May 31, 2024
@rgommers
Copy link
Copy Markdown
Member

Thanks, this is very helpful! I gave it a spin for the 1.13.1 docs - it's live on https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.vq.html now:

image

To enable:

$ ssh your-username@docs.scipy.org
$ cd /srv/docs_scipy_org/doc/scipy-1.13.1
$ vim try_examples.json  # edit the ignore list to remove: ".*"

Putting the pattern back immediately disables things again.

One thing I noticed is that the vertical height is too small. Changing it in try_examples.json from 400px to 600px doesn't seem to change anything. Is it possible that the 1.13.1 docs were built with a jupyterlite-sphinx version that doesn't handle min_height yet?

@lucascolley lucascolley changed the title DOC: Add documentation for how to enable interactive examples DOC/DEV: add docs for enabling interactive examples May 31, 2024
@rgommers
Copy link
Copy Markdown
Member

@agriyakhetarpal you'll be interested in this I'm sure (and may know the answer for the min_height question perhaps?).

@agriyakhetarpal
Copy link
Copy Markdown
Contributor

agriyakhetarpal commented May 31, 2024

One thing I noticed is that the vertical height is too small. Changing it in try_examples.json from 400px to 600px doesn't seem to change anything. Is it possible that the 1.13.1 docs were built with a jupyterlite-sphinx version that doesn't handle min_height yet?

Thanks for tagging me, @rgommers! The last paragraph in the Global Configuration section under the TryExamples directive page explains the reasons why a global minimum height that's set in conf.py does not apply to this directive. For a particular example, it has to be set manually if one has to change it (which I understand is convenient for an auto-generated doctest-based example; we could have a setting that sets a minimum height dynamically based on the amount of code content by tracking the number of lines and cells or something)

@rgommers
Copy link
Copy Markdown
Member

https://jupyterlite-sphinx.readthedocs.io/en/latest/directives/try_examples.html#global-min-height suggests we need global_min_height rather than min_height?

we could have a setting that sets a minimum height dynamically based on the amount of code content by tracking the number of lines and cells or something)

That's exactly what we don't want I think. The minimum height should be okay by default, it's much too small now. It works fine in the PyWavelets API reference docs.

@rgommers
Copy link
Copy Markdown
Member

Yep, global_min_height works (this is 600px):

image

Now we just need to pick the right value. 400px actually seems fine to me:

image

500px could also work; 600px seems a bit much (you can always open in a separate tab)

@steppi
Copy link
Copy Markdown
Member Author

steppi commented May 31, 2024

Ah, yes. Sorry about that. min_height instead of global_min_height is a bug in SciPy's try_examples.json. I can fix it in this PR.

@steppi
Copy link
Copy Markdown
Member Author

steppi commented May 31, 2024

I just added these specific commands @rgommers gave above to releasing.rst.inc

$ ssh your-username@docs.scipy.org
$ cd /srv/docs_scipy_org/doc/scipy-1.13.1
$ vim try_examples.json  # edit the ignore list to remove: ".*"

Copy link
Copy Markdown
Member

@melissawm melissawm left a comment

Choose a reason for hiding this comment

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

Thanks @steppi - this is looking good!

I have a few questions (and I'm not sure if they're in scope for users, but as a maintainer I think they are):

  • How is the kernel created and connected to our docs?
  • Where are the ipynb files generated by the docs build?
  • What is the path for a generated ipynb for a particular example/page? (this can be important also in the full page case like in #20303

@steppi
Copy link
Copy Markdown
Member Author

steppi commented May 31, 2024

Thanks Melissa!

  • How is the kernel created and connected to our docs?

I'm not aware of the underlying details but I know:

We have the jupyterlite-pyodide-kernel in our docs requirements here:

jupyterlite-pyodide-kernel

which get installed here

$(CURDIR)/build/env/bin/python -mpip install -r ../requirements/doc.txt

There's some metadata in the generated notebooks that tells the system to look for a Python kernel.

    nb["metadata"] = {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python",
        },
        "language_info": {
            "name": "python",
        },
    }
  • Where are the ipynb files generated by the docs build?

The path to these notebooks is scipy/doc/build/html/lite/files

  • What is the path for a generated ipynb for a particular example/page?

This might need some work for the sake of ergonomics. Each notebook's name is based on a uuid4 and these will be different each time the docs are generated. One can look in the generated html file for a piece of documentation, like
scipy/doc/build/html/reference/generated/scipy.integrate.quad.html

and find the try_examples_button

<button class="try_examples_button"
 onclick="window.tryExamplesShowIframe('732d240c-3ec6-4d9f-8c2b-8318deab1a79','284465d0-7adb-432b-995a-c1e126ab17b9','795d8fdc-a0ba-4c79-97ab-90de384b72b5','../../lite/tree/../notebooks/?path=7b2233aa_1a1d_40fe_9a23_4846c195450f.ipynb','None')">
Try it in your browser!
</button>

The name of the .ipynb file will be present there, and then you can find the file in the path above. I went with uuids to avoid clashes, but maybe that's not the most convenient thing.

@melissawm
Copy link
Copy Markdown
Member

So, does that mean we are using a vanilla kernel? I thought there was a step to build and select a specific kernel with a specific scipy version? Is that outdated info?

@steppi
Copy link
Copy Markdown
Member Author

steppi commented May 31, 2024

So, does that mean we are using a vanilla kernel? I thought there was a step to build and select a specific kernel with a specific scipy version? Is that outdated info?

Having a specific SciPy version is in the works, but isn’t ready yet. In the jupyterlite-sphinx docs I was just trying to point out that the problem exists. It’s much easier to solve for other projects though. SciPy is tough because of all of the Fortran.

Copy link
Copy Markdown
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Looks good to me for now. We've enabled interactive examples four days ago, and nothing went wrong yet. So let's call this good for now.

@rgommers rgommers merged commit d2b3067 into scipy:main Jun 4, 2024
@rgommers rgommers added this to the 1.15.0 milestone Jun 4, 2024
@lesteve
Copy link
Copy Markdown
Contributor

lesteve commented Jun 4, 2024

Nice to see this has been merged and all examples are enabled by default!

Not crucial but I was a bit confused originally because I was looking at the dev website and interactive examples are not enabled on the dev website for example https://scipy.github.io/devdocs/reference/generated/scipy.cluster.vq.vq.html#c58617e0-b72b-447b-b0cf-19e78ade50b3. I guess the reason is that you want it disabled by default and then enabled explicitly by sshing to the doc server and editing try_examples.json?

@rgommers
Copy link
Copy Markdown
Member

rgommers commented Jun 4, 2024

I guess the reason is that you want it disabled by default and then enabled explicitly by sshing to the doc server and editing try_examples.json?

Long term, no. But I think for now it's more confusing then helpful to default to adding the buttons, since any local changes to scipy won't be accessible from that button until we solve the problems related to nightly or local wheels for Pyodide.

@lesteve
Copy link
Copy Markdown
Contributor

lesteve commented Jun 4, 2024

Ah yeah good point indeed, I didn't think of this.

By the way there is likely a way to extend https://github.com/lesteve/scipy-tests-pyodide to run the Scipy doctests inside Pyodide and get an idea of what works and what doesn't, I need to look at it.

Minor side-comment: what you call examples (code examples in .py docstrings) are different than what scikit-learn call examples (gallery examples i.e. https://scikit-learn.org/stable/auto_examples) 😉

@rgommers
Copy link
Copy Markdown
Member

rgommers commented Jun 4, 2024

Re examples: yes indeed. We have longer-form content too in the User Guide, and that is still in the process (actually near the beginning of the process) to be migrated to executable content: https://scipy.github.io/devdocs/tutorial/index.html#executable-tutorials. Long-form .rst files (like this one for fft currently don't have buttons to run with JupyterLite.

By the way there is likely a way to extend https://github.com/lesteve/scipy-tests-pyodide to run the Scipy doctests inside Pyodide and get an idea of what works and what doesn't, I need to look at it.

That looks like quite magical hackery to make things work. Would the need for this be removed though by simply building a separately installable scipy-tests wheel? We just made that very easy and documented it at http://scipy.github.io/devdocs/building/redistributable_binaries.html#stripping-the-test-suite-from-a-wheel-or-installed-package.

@lesteve
Copy link
Copy Markdown
Contributor

lesteve commented Jun 4, 2024

That looks like quite magical hackery to make things work

Not sure which part you are talking about, but it certainly is a bit 😅.

A few things to note:

  • the main motivation was to test Pyodide main branch regularly to make sure that there were no regression in Scipy without noticing. The tests inside the Pyodide project are only testing Scipy functionality in a shallow manner.
  • Pyodide + packages are built in debug mode to try to have more informative Pyodide stack-traces
  • because running Scipy code can crash the Python interpreter (deterministic Pyodide fatal errors or random memory corruptions) the tests are run by submodule so that a fatal error in one of the submodules does not affect the rest of the test

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

Labels

Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org DX Everything related to making the experience of working on SciPy more pleasant

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants