Skip to content

Commit 05f7eb5

Browse files
committed
Conditionally load on pages
1 parent 3837897 commit 05f7eb5

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

docs/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
### Updates
66

7-
- `sphinx-thebe` now uses the correct and latest version of thebe, since it has been renamed from `thebelab` to `thebe`.
7+
- `sphinx-thebe` now uses the correct and latest version of thebe, since it has been renamed from `thebelab` to `thebe`.

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# "codemirror-theme": "blackboard" # Doesn't currently work
5151
}
5252

53-
myst_admonition_enable = True
53+
myst_enable_extensions = ["colon_fence"]
5454

5555
# Add any paths that contain templates here, relative to this directory.
5656
templates_path = ["_templates"]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"sphinx_thebe": ["_static/sphinx-thebe.css", "_static/sphinx-thebe.js",]
2828
},
2929
classifiers=["License :: OSI Approved :: MIT License"],
30-
install_requires=["sphinx>=1.8"],
30+
install_requires=["sphinx>=3.5,<5"],
3131
extras_require={
3232
"sphinx": [
3333
"myst-parser[sphinx]",

sphinx_thebe/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,25 @@ def init_thebe_default_config(app, env, docnames):
3030
thebe_config[key] = val
3131

3232

33-
def init_thebe_core(app, env):
34-
config_thebe = app.config["thebe_config"]
33+
def _check_if_load_thebe(doctree, config_thebe):
34+
"""Decide whether to load thebe based on the page's context."""
35+
# Only load `thebe` if there is a thebe button somewhere
36+
if not doctree or (not doctree.traverse(ThebeButtonNode)):
37+
return
38+
3539
if not config_thebe:
3640
logger.warning("Didn't find `thebe_config` in conf.py, add to use thebe")
3741
return
3842

43+
return True
44+
45+
46+
def init_thebe_core(app, pagename, templatename, context, doctree):
47+
"""Load thebe assets if there's a thebe button on this page."""
48+
config_thebe = app.config["thebe_config"]
49+
if not _check_if_load_thebe(doctree, config_thebe):
50+
return
51+
3952
# Add core libraries
4053
opts = {"async": "async"}
4154
app.add_js_file(filename="https://unpkg.com/thebe@latest/lib/index.js", **opts)
@@ -53,7 +66,7 @@ def init_thebe_core(app, env):
5366
def update_thebe_context(app, doctree, docname):
5467
"""Add thebe config nodes to this doctree."""
5568
config_thebe = app.config["thebe_config"]
56-
if not config_thebe:
69+
if not _check_if_load_thebe(doctree, config_thebe):
5770
return
5871

5972
# Thebe configuration
@@ -191,9 +204,10 @@ def setup(app):
191204
# Set default values for the configuration
192205
app.connect("env-before-read-docs", init_thebe_default_config)
193206

194-
# Include Thebe core docs
207+
# Update the doctree with thebe-specific information if needed
195208
app.connect("doctree-resolved", update_thebe_context)
196-
app.connect("env-updated", init_thebe_core)
209+
# Load the JS/CSS assets for thebe if needed
210+
app.connect("html-page-context", init_thebe_core)
197211

198212
# configuration for this tool
199213
app.add_config_value("thebe_config", {}, "html")

0 commit comments

Comments
 (0)