@@ -23,6 +23,7 @@ def st_static_path(app):
2323def init_thebe_default_config (app , env , docnames ):
2424 thebe_config = app .config .thebe_config
2525 defaults = {
26+ "always_load" : True ,
2627 "selector" : ".thebe" ,
2728 "selector_input" : "pre" ,
2829 "selector_output" : ".output" ,
@@ -32,28 +33,41 @@ def init_thebe_default_config(app, env, docnames):
3233 thebe_config [key ] = val
3334
3435
35- def _check_if_load_thebe (doctree , config_thebe ):
36+ def _bool (b ):
37+ if isinstance (b , bool ):
38+ return b
39+ else :
40+ return b in ["true" , "True" ]
41+
42+
43+ def _do_load_thebe (doctree , config_thebe ):
3644 """Decide whether to load thebe based on the page's context."""
37- # Only load `thebe` if there is a thebe button somewhere
38- if not doctree or (not doctree .traverse (ThebeButtonNode )):
39- return
45+ if not doctree :
46+ return False
4047
48+ # If we aren't properly configured
4149 if not config_thebe :
4250 logger .warning ("Didn't find `thebe_config` in conf.py, add to use thebe" )
43- return
51+ return False
4452
45- return True
53+ # Only load `thebe` if there is a thebe button somewhere
54+ if doctree .traverse (ThebeButtonNode ) or _bool (config_thebe .get ("always_load" )):
55+ return True
56+ else :
57+ return False
4658
4759
4860def init_thebe_core (app , pagename , templatename , context , doctree ):
4961 """Load thebe assets if there's a thebe button on this page."""
5062 config_thebe = app .config ["thebe_config" ]
51- if not _check_if_load_thebe (doctree , config_thebe ):
63+ if not _do_load_thebe (doctree , config_thebe ):
5264 return
5365
5466 # Add core libraries
5567 opts = {"async" : "async" }
56- app .add_js_file (filename = f"https://unpkg.com/thebe@{ THEBE_VERSION } /lib/index.js" , ** opts )
68+ app .add_js_file (
69+ filename = f"https://unpkg.com/thebe@{ THEBE_VERSION } /lib/index.js" , ** opts
70+ )
5771
5872 # Add configuration variables
5973 thebe_config = f"""
@@ -68,7 +82,7 @@ def init_thebe_core(app, pagename, templatename, context, doctree):
6882def update_thebe_context (app , doctree , docname ):
6983 """Add thebe config nodes to this doctree."""
7084 config_thebe = app .config ["thebe_config" ]
71- if not _check_if_load_thebe (doctree , config_thebe ):
85+ if not _do_load_thebe (doctree , config_thebe ):
7286 return
7387
7488 # Thebe configuration
@@ -208,11 +222,13 @@ def setup(app):
208222
209223 # Update the doctree with thebe-specific information if needed
210224 app .connect ("doctree-resolved" , update_thebe_context )
211- # Load the JS/CSS assets for thebe if needed
225+
226+ # Load the JS/CSS assets for thebe if needed
212227 app .connect ("html-page-context" , init_thebe_core )
213228
214229 # configuration for this tool
215230 app .add_config_value ("thebe_config" , {}, "html" )
231+
216232 # override=True in case Jupyter Sphinx has already been loaded
217233 app .add_directive ("thebe-button" , ThebeButton , override = True )
218234
0 commit comments