Skip to content

Commit 1d4be23

Browse files
Allow custom button texts for directives
1 parent 230ba7c commit 1d4be23

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def __init__(
119119
prefix=JUPYTERLITE_DIR,
120120
notebook=None,
121121
lite_options={},
122+
button_text=None,
122123
**attributes,
123124
):
124125
app_path = self.lite_app
@@ -133,6 +134,8 @@ def __init__(
133134
f'{prefix}/{app_path}{f"index.html?{options}" if options else ""}'
134135
)
135136

137+
self.button_text = button_text
138+
136139
super().__init__(
137140
rawsource,
138141
**attributes,
@@ -142,7 +145,7 @@ def html(self):
142145
return (
143146
'<button class="try_examples_button" '
144147
f"onclick=\"window.open('{self.lab_src}')\">"
145-
"Open as a notebook</button>"
148+
f"{self.button_text}</button>"
146149
)
147150

148151

@@ -210,6 +213,7 @@ class BaseNotebookTab(_InTab):
210213

211214
lite_app = None
212215
notebooks_path = None
216+
default_button_text = "Open as a notebook"
213217

214218

215219
class JupyterLiteTab(BaseNotebookTab):
@@ -295,6 +299,7 @@ def __init__(
295299
prefix=JUPYTERLITE_DIR,
296300
notebook=None,
297301
lite_options={},
302+
button_text=None,
298303
**attributes,
299304
):
300305

@@ -308,6 +313,8 @@ def __init__(
308313
# If a notebook is provided, open it in a new tab. Else, we default to the tree view.
309314
self.lab_src = f'{prefix}/{app_path}{f"?{options}" if options else ""}'
310315

316+
self.button_text = button_text
317+
311318
super().__init__(
312319
rawsource,
313320
**attributes,
@@ -317,7 +324,7 @@ def html(self):
317324
return (
318325
'<button class="try_examples_button" '
319326
f"onclick=\"window.open('{self.lab_src}')\">"
320-
"Open with Voici</button>"
327+
f"{self.button_text}</button>"
321328
)
322329

323330

@@ -380,6 +387,7 @@ class _LiteDirective(SphinxDirective):
380387
"prompt_color": directives.unchanged,
381388
"search_params": directives.unchanged,
382389
"new_tab": directives.unchanged,
390+
"button_text": directives.unchanged,
383391
}
384392

385393
def run(self):
@@ -393,6 +401,8 @@ def run(self):
393401

394402
new_tab = self.options.pop("new_tab", False)
395403

404+
button_text = None
405+
396406
source_location = os.path.dirname(self.get_source_info()[0])
397407

398408
prefix = os.path.relpath(
@@ -440,6 +450,22 @@ def run(self):
440450
else:
441451
notebook_name = None
442452

453+
if new_tab:
454+
directive_button_text = self.options.pop("button_text", None)
455+
if directive_button_text is not None:
456+
button_text = directive_button_text
457+
else:
458+
# If none, we use the appropriate global config based on
459+
# the type of directive passed.
460+
if isinstance(self, JupyterLiteDirective):
461+
button_text = self.env.config.jupyterlite_button_text
462+
elif isinstance(self, NotebookLiteDirective):
463+
button_text = self.env.config.notebooklite_button_text
464+
elif isinstance(self, VoiciDirective):
465+
button_text = self.env.config.voici_button_text
466+
elif "button_text" in self.options:
467+
raise ValueError("'button_text' is only valid if 'new_tab' is True. To modify the prompt text, use 'prompt' and 'prompt_color'.")
468+
443469
if new_tab:
444470
return [
445471
self.newtab_cls(
@@ -451,6 +477,7 @@ def run(self):
451477
prompt_color=prompt_color,
452478
search_params=search_params,
453479
lite_options=self.options,
480+
button_text=button_text,
454481
)
455482
]
456483

@@ -482,6 +509,9 @@ class BaseJupyterViewDirective(_LiteDirective):
482509
"prompt_color": directives.unchanged,
483510
"search_params": directives.unchanged,
484511
"new_tab": directives.unchanged,
512+
# "button_text" below is valid only if "new_tab" is True, otherwise
513+
# we have "prompt" and "prompt_color" as options already.
514+
"button_text": directives.unchanged,
485515
}
486516

487517

@@ -895,6 +925,12 @@ def setup(app):
895925
rebuild="html",
896926
)
897927

928+
# Allow customising the button text for each directive (only when "new_tab" is True,
929+
# error otherwise)
930+
app.add_config_value("jupyterlite_button_text", "Open as a notebook", rebuild="html")
931+
app.add_config_value("notebooklite_button_text", "Open as a notebook", rebuild="html")
932+
app.add_config_value("voici_button_text", "Open with Voici", rebuild="html")
933+
898934
# Initialize NotebookLite and JupyterLite directives
899935
app.add_node(
900936
NotebookLiteIframe,

0 commit comments

Comments
 (0)