|
50 | 50 | - ``show_memory`` (:ref:`show_memory`) |
51 | 51 | - ``show_signature`` (:ref:`show_signature`) |
52 | 52 | - ``binder`` (:ref:`binder_links`) |
| 53 | +- ``jupyterlite`` (:ref:`jupyterlite`) |
53 | 54 | - ``promote_jupyter_magic`` (:ref:`promote_jupyter_magic`) |
54 | 55 | - ``first_notebook_cell`` and ``last_notebook_cell`` (:ref:`own_notebook_cell`) |
55 | 56 | - ``notebook_images`` (:ref:`notebook_images`) |
@@ -1117,6 +1118,108 @@ See the Sphinx-Gallery `Sphinx configuration file |
1117 | 1118 | <https://github.com/sphinx-gallery/sphinx-gallery/blob/master/doc/conf.py>`_ |
1118 | 1119 | for an example that uses the `public Binder server <https://mybinder.org>`_. |
1119 | 1120 |
|
| 1121 | +.. _jupyterlite: |
| 1122 | + |
| 1123 | +Generate JupyterLite links for gallery notebooks (experimental) |
| 1124 | +=============================================================== |
| 1125 | + |
| 1126 | +Sphinx-Gallery automatically generates Jupyter notebooks for any examples built |
| 1127 | +with the gallery. `JupyterLite <https://jupyterlite.readthedocs.io>`__ makes it |
| 1128 | +possible to run an example in your browser. The functionality is quite similar |
| 1129 | +to Binder in the sense that you will get a Jupyter environment where you can |
| 1130 | +run the example interactively as a notebook. The main difference with Binder |
| 1131 | +are: |
| 1132 | + |
| 1133 | +- with JupyterLite, the example actually runs in your browser, there is no need |
| 1134 | + for a separate machine in the cloud to run your Python code. That means that |
| 1135 | + starting a Jupyter server is genrally quicker, no need to wait for the Binder |
| 1136 | + image to be built |
| 1137 | +- with JupyterLite the first imports take time. At the time of writing |
| 1138 | + (February 2023) ``import scipy`` can take ~15-30s. Some innocuously looking |
| 1139 | + Python code may just not work and break in an unexpected fashion. The Jupyter |
| 1140 | + kernel is based on Pyodide, see some `here |
| 1141 | + <https://pyodide.org/en/latest/usage/wasm-constraints.html>`__ for some |
| 1142 | + Pyodide limitations. |
| 1143 | +- with JupyterLite environments are not as flexible as Binder, for example you |
| 1144 | + can not use a docker image but only the default `Pyodide |
| 1145 | + <https://pyodide.org/en/stable/index.html>`__ environment. That means that |
| 1146 | + some non pure-Python packages may not be available, see list of `available |
| 1147 | + packages in Pyodide |
| 1148 | + <https://pyodide.org/en/stable/usage/packages-in-pyodide.html>`__. |
| 1149 | + |
| 1150 | +.. warning:: |
| 1151 | + |
| 1152 | + JupyterLite is still beta technology and less mature than Binder, so there |
| 1153 | + may be instability or unexpected behaviour in the experience of users who |
| 1154 | + click JupyterLite links. |
| 1155 | + |
| 1156 | +In order to enable JupyterLite links with Sphinx-Gallery, you must install the |
| 1157 | +`jupyterlite-sphinx <https://jupyterlite-sphinx.readthedocs.io>`_ package and |
| 1158 | +add it to your extensions in ``conf.py``:: |
| 1159 | + |
| 1160 | + extensions = [ |
| 1161 | + ..., |
| 1162 | + ..., |
| 1163 | + ..., |
| 1164 | + 'jupyterlite_sphinx', |
| 1165 | + ] |
| 1166 | + |
| 1167 | +You can configure JupyterLite integration by setting |
| 1168 | +``sphinx_gallery_conf['jupyterlite']`` in ``conf.py`` like this:: |
| 1169 | + |
| 1170 | + sphinx_gallery_conf = { |
| 1171 | + ... |
| 1172 | + 'jupyterlite': { |
| 1173 | + 'jupyterlite_contents': <str> # JupyterLite contents where to copy the example notebooks (relative to Sphinx source directory) |
| 1174 | + 'use_jupyter_lab': <bool> # Whether JupyterLite links should start Jupyter Lab instead of the Retrolab Notebook interface. |
| 1175 | + } |
| 1176 | + } |
| 1177 | + |
| 1178 | +Below is a more complete explanation of each field. |
| 1179 | + |
| 1180 | +use_jupyter_lab (type: bool, default: ``True``) |
| 1181 | + Whether the default interface activated by the JupyterLite link will be for |
| 1182 | + Jupyter Lab or the RetroLab Notebook interface. |
| 1183 | + |
| 1184 | +jupyterlite_contents (type: string, default: ``jupyterlite_contents``) The name |
| 1185 | + of a folder where the built Jupyter notebooks will be copied, relative to the |
| 1186 | + Sphinx source directory. This is used as Jupyterlite contents. |
| 1187 | + |
| 1188 | +You can set variables in ``conf.py`` to configure ``jupyterlite-sphinx``, see |
| 1189 | +its `jupyterlite-sphinx doc |
| 1190 | +<https://jupyterlite-sphinx.readthedocs.io/en/latest/configuration.html>`__ for |
| 1191 | +more details. |
| 1192 | + |
| 1193 | +If a Sphinx-Gallery configuration for JupyterLite is discovered, the following |
| 1194 | +extra things will happen: |
| 1195 | + |
| 1196 | +1. Configure ``jupyterlite-sphinx`` with some reasonable defaults, e.g. set |
| 1197 | + ``jupyterlite_bind_ipynb_suffix = False``. |
| 1198 | +2. The built Jupyter Notebooks from the documentation will be copied to a |
| 1199 | + folder called ``<jupyterlite_contents>/`` (relative to Sphinx source |
| 1200 | + directory) |
| 1201 | +3. The rST output of each Sphinx-Gallery example will now have a |
| 1202 | + ``launch JupyterLite`` button in it. |
| 1203 | +4. That button will point to a JupyterLite link which will start a Jupyter |
| 1204 | + server in your browser with the current example as notebook |
| 1205 | + |
| 1206 | +If, for some reason, you want to enable the ``jupyterlite-sphinx`` extension |
| 1207 | +but not use sphinx-gallery Jupyterlite integration you can do:: |
| 1208 | + |
| 1209 | + extensions = [ |
| 1210 | + ..., |
| 1211 | + jupyterlite_sphinx, |
| 1212 | + ] |
| 1213 | + |
| 1214 | + sphinx_gallery_conf = { |
| 1215 | + ... |
| 1216 | + 'jupyterlite': None |
| 1217 | + } |
| 1218 | + |
| 1219 | +See the Sphinx-Gallery `Sphinx configuration file |
| 1220 | +<https://github.com/sphinx-gallery/sphinx-gallery/blob/master/doc/conf.py>`_ |
| 1221 | +for an example that uses the JupyterLite integration. |
| 1222 | + |
1120 | 1223 | .. _promote_jupyter_magic: |
1121 | 1224 |
|
1122 | 1225 | Making cell magic executable in notebooks |
|
0 commit comments