Sphinx Thebe
Sphinx Thebe is a Sphinx extension that integrates interactive code blocks into documentation using Thebe.js and Binder. It allows readers to execute code examples directly in their browser against a live Jupyter kernel. The current version is 0.3.1, with releases occurring periodically to address Sphinx compatibility and feature enhancements.
Common errors
-
ModuleNotFoundError: No module named 'sphinx_thebe'
cause The `sphinx-thebe` package has not been installed in your Python environment or is not accessible to your Sphinx build.fixInstall the package using pip: `pip install sphinx-thebe`. -
Interactive code blocks do not appear or the 'Run' button doesn't work.
cause This can be due to several reasons: missing `thebe-button` directive, incorrect `thebe_config` in `conf.py`, network issues preventing connection to BinderHub, or a misconfigured kernel.fix1. Ensure a `.. thebe-button::` directive (or equivalent for MyST) precedes your code block. 2. Verify `thebe_config` in `conf.py` is correctly defined and points to valid resources (e.g., `binderhub_url`, `repository_url`). 3. Check your browser's developer console for network errors or JavaScript issues. 4. Confirm your Binder repository and specified kernel are functioning correctly. -
Thebe cells always show 'Initializing...' but never connect or run code.
cause This typically indicates a problem with the connection to the BinderHub, either due to an incorrect URL, a blocked connection, or an issue with the Binder instance itself.fixDouble-check the `binderhub_url` in your `thebe_config` in `conf.py`. Try accessing the BinderHub directly in your browser. Ensure there are no network restrictions (e.g., corporate firewalls) preventing your browser from connecting to the BinderHub URL. Review the BinderHub logs if you are hosting it yourself.
Warnings
- gotcha Sphinx-thebe requires a configured BinderHub instance (e.g., mybinder.org) to fetch live kernels. If `binderhub_url` and `repository_url` are not correctly set in `thebe_config`, interactive cells will not function.
- gotcha By default, Thebe.js is lazily loaded, meaning interactive buttons only appear and become active after an initial click. Users expecting immediate interactivity on page load might be confused.
- gotcha If you are heavily customizing Thebe's frontend with custom JavaScript that directly manipulates its DOM elements or relies on specific jQuery versions, updates in `sphinx-thebe` (like the v0.3.1 jQuery fix) could cause unexpected behavior.
- gotcha Interactive code execution implies running arbitrary code from a remote source. Ensure users understand the security implications, especially if your BinderHub is self-hosted or allows user-defined environments.
Install
-
pip install sphinx-thebe
Imports
- sphinx_thebe
extensions = ['sphinx_thebe']
Quickstart
# 1. Install sphinx-thebe:
# pip install sphinx-thebe
# 2. In your Sphinx project's conf.py, add 'sphinx_thebe' to extensions:
# extensions = [
# 'sphinx_thebe'
# ]
# 3. Configure thebe_config (replace with your BinderHub URL and kernel):
# thebe_config = {
# 'repository_url': 'https://github.com/binder-examples/jupyter-stacks-datascience',
# 'repository_branch': 'master',
# 'binderhub_url': 'https://mybinder.org',
# 'kernel_name': 'python3',
# 'code_selector': 'div.highlight',
# 'always_load': false
# }
# 4. In a reStructuredText file (.rst) or MyST Markdown file (.md) write:
# .. thebe-button::
# :label: Run this code!
# .. code-block:: python
# print('Hello, Thebe!')
# x = 1 + 1
# print(f'x is {x}')
# 5. Build your Sphinx documentation:
# make html