Skip to content

Conversation

steppi
Copy link
Contributor

@steppi steppi commented Feb 3, 2024

Reference issue

Closes #19729

What does this implement/fix?

This PR adds interactive examples using the TryExamplesDirective from jupyterlite-sphinx. This directive adds a button which can be pressed to swap doctest Examples sections in-place with embedded Jupyterlite notebooks. These notebooks run locally in the user's browser, adding interactivity to the examples.

Additional information

Here is a screenshot of the button

image

and here is a screenshot showing how things look after the button is pressed

image

I have an experimental deployment up at https://steppi.github.io/scipy/, if anyone wants to try it out before the docs finish building, or if the docs don't build for some reason.

@steppi steppi requested review from tupui and rgommers as code owners February 3, 2024 16:26
@github-actions github-actions bot added the Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org label Feb 3, 2024
@steppi
Copy link
Contributor Author

steppi commented Feb 3, 2024

There's a bug in the processing of literal blocks in RST which can be seen at https://steppi.github.io/scipy/reference/generated/scipy.integrate.quad.html#scipy.integrate.quad. I've made a pull request to jupyterlite-sphinx to fix this here jupyterlite/jupyterlite-sphinx#134.

Update: The example is now fixed at the link above, but you can see a screenshot of what it looked like in the linked PR.

@steppi
Copy link
Contributor Author

steppi commented Feb 3, 2024

This is set up to use the jupyterlite pyodide kernel. SciPy 1.12 is not available for it yet, but when it is this could be backported to 1.12. Before merging, I could disable the interactive examples by adding ignore patterns to the file try_examples.json. The interactive examples can be re-enabled at run time by editing the version of this file which is copied to the build directory of the deployed docs.

Also, the pyodide kernel loads very slowly. Executing the first cell of a notebook can take 30 seconds or more, which may be annoying to users. It may be better to switch to the xeus-kernel which has faster load times. jupyterlite-sphinx uses xeus for its own docs, jupyterlite/jupyterlite-sphinx#98. @martinRenou, do you have an opinion on the choice of kernel, and an idea of how difficult it would be to make an up-to-date SciPy available in xeus for the SciPy docs?

@steppi steppi changed the title DOC: Add support for interactive examples [docs only] DOC: Add support for interactive examples with jupyterlite-sphinx Feb 3, 2024
@steppi
Copy link
Contributor Author

steppi commented Feb 3, 2024

Interesting, it's not working in the circle ci deployment here. It works locally though. Marking as draft. I'll try to figure it out.

@steppi steppi marked this pull request as draft February 3, 2024 17:17
@tupui
Copy link
Member

tupui commented Feb 3, 2024

This is great thank you @steppi! I did a quick test on mobile and it also works there 👍

For Kernel choices and other tweaks, I would do the strict minimum to have the smallest maintenance possible. We tend to customize things a bit too much making it tedious to maintain.

For CircleCI, I think it's not an issue at all as we just use the rendering there to check that new docs render properly.

@steppi steppi marked this pull request as ready for review February 3, 2024 18:46
@steppi
Copy link
Contributor Author

steppi commented Feb 3, 2024

Thanks @tupui.

For Kernel choices and other tweaks, I would do the strict minimum to have the smallest maintenance possible. We tend to customize things a bit too much making it tedious to maintain.

OK, cool. I'm not sure yet which would have the lower maintenance burden between xeuss and pyodide, but it makes sense to pick the the one with less.

For CircleCI, I think it's not an issue at all as we just use the rendering there to check that new docs render properly.

Good to know. I thought maybe it was a sign the extension wasn't working as intended, but it seems to be due to limitations in circle ci's rendered docs.

@martinRenou
Copy link

do you have an opinion on the choice of kernel, and an idea of how difficult it would be to make an up-to-date SciPy available in xeus for the SciPy docs?

You can already make a jupyterlite-xeus deployment with scipy 1.11 and xeus-python. I can assist you into doing it, it's quite easy actually, you just need to install jupyterlite-xeus and provide an environment.yml file containing your docs dependencies.

I don't know how hard it would be to get the latest scipy on emscripten-forge (the conda channel used for the WASM environment), we should give it a try.

@steppi
Copy link
Contributor Author

steppi commented Feb 4, 2024

Thanks @martinRenou. It seems like jupyterlite-xeus is the way to go. Yeah, let’s look into how to get the latest SciPy into emscripten-forge.

@steppi steppi force-pushed the interactive-docs branch from fadacba to b3ecdd0 Compare March 8, 2024 02:47
@steppi
Copy link
Contributor Author

steppi commented Mar 8, 2024

I think this is in good shape now and ready to merge if anyone wants to take a look at it. I've configured it to disable all of the interactive examples, this is just putting the framework in place, so it should be safe to do.

Some more info.

Scipy 1.12 hasn't made it's way into pyodide yet, though the PR for adding it, pyodide/pyodide#4499, has been approved. I mentioned the possibility of using the jupyterlite-xeus kernel above, but from my experiments with it, it seems not to make an appreciable difference in load times when importing SciPy. It is also further behind in the supported SciPy versions.

There have been some updates in jupyterlite-sphinx since I last posted an example.

  1. The first cell now contains a warning text, warning of potentially large downloads, long load times, ...

image

This idea was borrowed from scikit-learn's jupyterlite interactive examples.

  1. As discussed here, DOC: Add interactive examples with jupyterlite-sphinx #19729, the interactive examples have been disabled on sufficiently small screens as a heuristic to avoid large downloads on limited data mobile plans.

You can try it out at the latest deployment at the link below

https://steppi.github.io/scipy/reference/

for a direct link to a specific example see

https://steppi.github.io/scipy/reference/generated/scipy.integrate.quad.html#scipy.integrate.quad

try_examples_global_warning_text = (
"SciPy's interactive examples with Jupyterlite are experimental and may"
" not always work as expected. Execution of cells containing imports may"
" result in large downloads (up to 60MB of content for the first import"

Choose a reason for hiding this comment

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

Maybe adding a note that those data will then be cached by the browser, so the following openings of notebooks will not download those data again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh. I wonder if it's something to do with the tracking settings I have in firefox, but the caching doesn't work for me. Every time I import scipy in a different examples notebook, it downloads scipy, numpy, and openblas again.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I would keep this as is. These are "details" and until we remove the experimental note we should not give more guarantee. At most i think we could add a link to the underlying tech.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been playing around with the network tab in the Firefox web console. I've turned off all tracking protection in firefox, and the caching still doesn't work for me. Every time I execute an import from scipy in a different notebook, scipy, numpy, and openblas get downloaded again. Also, total download size from hitting the button to open the notebook and importing SciPy is actually more like 130MB. I was just adding up the size of the numpy, scipy, and openblas wheels that get downloaded before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something weird was going on with the 130MB, I haven't witnessed that again

Copy link
Member

@tupui tupui 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, I left some minor suggestions. I will find some time next week to try. But we should just get this in 😃

Copy link
Member

Choose a reason for hiding this comment

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

Can you instead move this to scipy.css? I would prefer if we keep a single file for custom css

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that should work.

"jupytext",
"myst-nb",
"pooch",
"jupyterlite-sphinx>=0.12.0",
Copy link
Member

Choose a reason for hiding this comment

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

Please also add these in the environment.yml and also in the doc about the toolchain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I missed this.

try_examples_global_warning_text = (
"SciPy's interactive examples with Jupyterlite are experimental and may"
" not always work as expected. Execution of cells containing imports may"
" result in large downloads (up to 60MB of content for the first import"
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I would keep this as is. These are "details" and until we remove the experimental note we should not give more guarantee. At most i think we could add a link to the underlying tech.

@steppi steppi added this to the 1.13.0 milestone Mar 8, 2024
@steppi
Copy link
Contributor Author

steppi commented Mar 8, 2024

Thanks @tupui! I've heard branching for 1.13 could be imminent, so agreed, let's just get this in.

Copy link
Member

@tupui tupui left a comment

Choose a reason for hiding this comment

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

All good to me 👍 thanks again Albert 🚀

@tupui tupui merged commit 7977e9e into scipy:main Mar 8, 2024
@steppi
Copy link
Contributor Author

steppi commented Mar 8, 2024

PR to get SciPy 1.12 into pyodide is merged, pyodide/pyodide#4499. When that makes it into a release I'll submit a follow-up to pin the version

@martinRenou
Copy link

Super nice thanks!

@rgommers rgommers added the enhancement A new feature or improvement label Mar 11, 2024
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 enhancement A new feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Add interactive examples with jupyterlite-sphinx
4 participants