Interactive content: Run Python inside your book#
User types
This section is useful for user type 5.
Our book has been enable to run Python code live in the browser (thanks Max!). This extension makes code cells in your .ipynb
book pages editable and executable by the reader! Opposed to the original sphinx-thebe extensions, our extension runs python in the reader’s browser and doesn’t rely on an external webserver for the python kernel. We highly recommend this extension as it allows you to use the strength of combining text/figures and code of a jupyter book with interactivity!
This page contains some installation instructions and the other sections show how to use this functionality to create interactive figures and feedback on code
To see this happening, click –> Live Code on the top right corner of this page
print('hello world')
Setting up Python live coding#
To set up the Python live coding you need to add our own sphinx-thebe extension to your book. This extensions doesn’t rely on a 3rd party like Binder and it supports local python execution and custom features. Therefore, you need to add some lines to requirements.txt
and _config.yml
For requirements.txt
add the following lines:
--extra-index-url https://gitlab.tudelft.nl/api/v4/projects/11239/packages/pypi/simple
sphinx-thebe ~= 0.9.9
This will download the correct version of the sphinx extension when the book is built on the server (which loads the required packages from requirements.txt
)
If you want to build the book locally, you need to install this version of sphinx-thebe in your environment as well with:
pip install -r requirements.txt
Afterwards, this sphinx extension needs to be enabled in your book. This can be done by adding the following lines to _config.yml
:
launch_buttons:
thebe: true
sphinx:
config:
html_js_files:
- https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js
thebe_config:
use_thebe_lite: true
exclude_patterns: ["**/_*.yml", "**/*.md", "**/*.ipynb"]
The html_js_files
link calls for some required javascript files. use_thebe_lite
makes sure you initiate our adapted sphinx thebe extensions. The exclude_patterns
makes sure you import all files so that they can be accessed from your code, except for the files matching the patterns shown.
Note
If you’ve a lot of big files which are not used by the code, the build book repository might grow a lot in size. Include unnecessary filetypes like images (i.e. "**/*.png
) to exclude_patterns
. Make sure you use proper markdown or MyST syntax for referencing to images. Using HTML code might lead to figures not being available in the final book.
By launch_buttons
you initiate the –> Live Code on the top right corner of every page generated from a .ipynb
file.
Note, if you’re messing around with html_theme_options
(for example when adding more buttons), all button behaviour is affected. In that case, as opposed to the jupyter book documentation, all buttons have to be specified from within sphinx: html_theme_options
where you might also specify other buttons as well (ie. the code repository button).
sphinx:
config:
html_js_files:
- https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js
thebe_config:
use_thebe_lite: true
exclude_patterns: ["**/_*.yml", "**/*.md", "**/*.ipynb"]
html_theme_options:
repository_url: "hello!"
use_repository_button: true
launch_buttons:
thebe: true
Instructions: local build#
To test the fuctionality, you have to run a local server, otherwise the interactivity doens’t work. So, you cannot just open the index.html
file any more. This can be done with the steps shown in setup-local-server.
If you push to main, you can test the interactivity on the book-draft website as well.
Additional packages#
The python kernel doesn’t have all packages standard included. Some of the most used packages which are included are:
python
numpy
scipy
sympy
matplotlib
ipywidgets
If you’d like to install more package, you can do so by added a codecell (preferably hidden using thebe-remove-input-init
) with the following command:
import micropip
await micropip.install("package_name")
This will install the packages: Micropip will look at the Pyodide package index, but also at the general PyPi index. If a package is pure python (i.e. no C extensions), then it can also be used by Pyodide.
Custom packages#
If you have some custom packages you can also create a wheel to import. Therefore, create a wheel from your package by running this in the root directoy of your package:
python setup.py bdist_wheel
Copy the wheel from dist/<something>.whl
to your book and import the package from within a notebook using:
import micropip
await micropip.install('<directory>/<something>.whl')
Preferably with the tag thebe-remove-input-init
.
Other packages not in Pyodide but on PyPI#
If the packages are not included in Pyodide, you can use `pip:
%pip install package_name
Alternative with JupyterLite (less well integrated)#
Another option to use python in your browser is to use JupyterLite. However, this doesn’t give a similar seamless interface. You could create ‘iframe-like’ Ipython consoles / jupyterbook / jupyter lab environments in your book. As these ‘iframes’ are separate modules added to your page, the integration with the other content in your book is less flexible as with our sphinx-thebe extension. Furthermore, the best way to use a JupyterLite session would be a separate repo with the jupyter lab interface anyway, so you get the full browser experience. Alternatively, the ipython REPL better can be usefull, as shown here: https://teachbooks.io/learn-python/main/Python_Toolbox.html.