-
Notifications
You must be signed in to change notification settings - Fork 211
Jupyterlite integration #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f7d5378
4696bae
6e41ab5
e02f846
e9db8e3
ddaefcf
6577a80
e79861b
6ea841b
398c0bb
8626d3e
473f290
e75204f
8864923
f632a59
e7e360f
24d4b73
3a493bc
aecb020
946f374
ccf4850
b59ba44
84b5464
2a9570a
8cee6e3
2c8068a
c058673
97e4967
ac520c2
5fd4307
4f29129
d0c9741
dd338e7
7615440
67389dc
1387b25
4b6c9f6
a6c6dcd
7d420af
fb17377
2ff6f27
4cd2d46
870a5cc
66e4d9a
fd2ceb5
884fd38
feb9d27
3e0405a
a65cbf2
60a0f7f
559075e
917f75b
b12a0ce
a68b279
95b835c
d1ddb6c
5d4a036
5ed1e8d
8686749
ed69e84
8dba807
106d97c
cae1b43
375df49
d88c716
05c6673
23dc9ca
ba78add
7b6c2c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,6 @@ target/ | |
|
||
# Jupyter notebooks | ||
.ipynb_checkpoints | ||
|
||
# Default JupyterLite content | ||
jupyterlite_contents |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ plotly | |
absl-py | ||
graphviz | ||
packaging | ||
jupyterlite-sphinx |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ file: | |
- ``show_memory`` (:ref:`show_memory`) | ||
- ``show_signature`` (:ref:`show_signature`) | ||
- ``binder`` (:ref:`binder_links`) | ||
- ``jupyterlite`` (:ref:`jupyterlite`) | ||
- ``promote_jupyter_magic`` (:ref:`promote_jupyter_magic`) | ||
- ``first_notebook_cell`` and ``last_notebook_cell`` (:ref:`own_notebook_cell`) | ||
- ``notebook_images`` (:ref:`notebook_images`) | ||
|
@@ -1116,6 +1117,108 @@ See the Sphinx-Gallery `Sphinx configuration file | |
<https://github.com/sphinx-gallery/sphinx-gallery/blob/master/doc/conf.py>`_ | ||
for an example that uses the `public Binder server <https://mybinder.org>`_. | ||
|
||
.. _jupyterlite: | ||
|
||
Generate JupyterLite links for gallery notebooks (experimental) | ||
=============================================================== | ||
|
||
Sphinx-Gallery automatically generates Jupyter notebooks for any examples built | ||
with the gallery. `JupyterLite <https://jupyterlite.readthedocs.io>`__ makes it | ||
possible to run an example in your browser. The functionality is quite similar | ||
to Binder in the sense that you will get a Jupyter environment where you can | ||
run the example interactively as a notebook. The main difference with Binder | ||
are: | ||
|
||
- with JupyterLite, the example actually runs in your browser, there is no need | ||
for a separate machine in the cloud to run your Python code. That means that | ||
starting a Jupyter server is genrally quicker, no need to wait for the Binder | ||
image to be built | ||
- with JupyterLite the first imports take time. At the time of writing | ||
(February 2023) ``import scipy`` can take ~15-30s. Some innocuously looking | ||
Python code may just not work and break in an unexpected fashion. The Jupyter | ||
kernel is based on Pyodide, see some `here | ||
<https://pyodide.org/en/latest/usage/wasm-constraints.html>`__ for some | ||
Pyodide limitations. | ||
- with JupyterLite environments are not as flexible as Binder, for example you | ||
can not use a docker image but only the default `Pyodide | ||
<https://pyodide.org/en/stable/index.html>`__ environment. That means that | ||
some non pure-Python packages may not be available, see list of `available | ||
packages in Pyodide | ||
<https://pyodide.org/en/stable/usage/packages-in-pyodide.html>`__. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could expand this point a bit and explain that this it's possible to install extra, pure-python packages from pypi.org using the following construct: try:
# Manually install extra dependencies when running this example with
# Pyodide:
import micropip
await micropip.install("package_name")
except ImportError:
# Assume all the dependencies are already installed when running on other
# types of hosts, such as binder.
pass Not protecting this snippet against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the
Which could help make remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice I didn't know this! |
||
|
||
.. warning:: | ||
|
||
JupyterLite is still beta technology and less mature than Binder, so there | ||
may be instability or unexpected behaviour in the experience of users who | ||
click JupyterLite links. | ||
|
||
In order to enable JupyterLite links with Sphinx-Gallery, you must install the | ||
`jupyterlite-sphinx <https://jupyterlite-sphinx.readthedocs.io>`_ package and | ||
add it to your extensions in ``conf.py``:: | ||
|
||
extensions = [ | ||
..., | ||
..., | ||
..., | ||
'jupyterlite_sphinx', | ||
] | ||
|
||
You can configure JupyterLite integration by setting | ||
``sphinx_gallery_conf['jupyterlite']`` in ``conf.py`` like this:: | ||
|
||
sphinx_gallery_conf = { | ||
... | ||
'jupyterlite': { | ||
'jupyterlite_contents': <str> # JupyterLite contents where to copy the example notebooks (relative to Sphinx source directory) | ||
'use_jupyter_lab': <bool> # Whether JupyterLite links should start Jupyter Lab instead of the Retrolab Notebook interface. | ||
} | ||
} | ||
|
||
Below is a more complete explanation of each field. | ||
|
||
use_jupyter_lab (type: bool, default: ``True``) | ||
Whether the default interface activated by the JupyterLite link will be for | ||
Jupyter Lab or the RetroLab Notebook interface. | ||
|
||
jupyterlite_contents (type: string, default: ``jupyterlite_contents``) The name | ||
of a folder where the built Jupyter notebooks will be copied, relative to the | ||
Sphinx source directory. This is used as Jupyterlite contents. | ||
|
||
You can set variables in ``conf.py`` to configure ``jupyterlite-sphinx``, see | ||
its `jupyterlite-sphinx doc | ||
<https://jupyterlite-sphinx.readthedocs.io/en/latest/configuration.html>`__ for | ||
more details. | ||
|
||
If a Sphinx-Gallery configuration for JupyterLite is discovered, the following | ||
extra things will happen: | ||
|
||
1. Configure ``jupyterlite-sphinx`` with some reasonable defaults, e.g. set | ||
``jupyterlite_bind_ipynb_suffix = False``. | ||
2. The built Jupyter Notebooks from the documentation will be copied to a | ||
folder called ``<jupyterlite_contents>/`` (relative to Sphinx source | ||
directory) | ||
3. The rST output of each Sphinx-Gallery example will now have a | ||
``launch JupyterLite`` button in it. | ||
4. That button will point to a JupyterLite link which will start a Jupyter | ||
server in your browser with the current example as notebook | ||
|
||
If, for some reason, you want to enable the ``jupyterlite-sphinx`` extension | ||
but not use sphinx-gallery Jupyterlite integration you can do:: | ||
|
||
extensions = [ | ||
..., | ||
jupyterlite_sphinx, | ||
] | ||
|
||
sphinx_gallery_conf = { | ||
... | ||
'jupyterlite': None | ||
} | ||
|
||
See the Sphinx-Gallery `Sphinx configuration file | ||
<https://github.com/sphinx-gallery/sphinx-gallery/blob/master/doc/conf.py>`_ | ||
for an example that uses the JupyterLite integration. | ||
|
||
.. _promote_jupyter_magic: | ||
|
||
Making cell magic executable in notebooks | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there could be a quick mention that it's also be possible to use other kernels, for example Xeus Python: https://github.com/jupyterlite/xeus-python-kernel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good point. I tried to focus this PR on the simplest case i.e. the Pyolite kernel, but this is certainly something which could be mentioned in a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem it could definitely be mentioned later (just noticed while looking at the diff).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Especially with deeper instructions on how to package a kernel with built-in dependencies required to run the examples of a given project.