@@ -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):
5366def 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