Skip to content

Commit 6a69b46

Browse files
authored
Merge pull request #227 from agriyakhetarpal/feat/customise-new-tab-buttons-text
Allow global and custom button text for the new-tabbed variants of the `JupyterLite`, `NotebookLite`, and the `Voici` directives
2 parents 230ba7c + eee1d68 commit 6a69b46

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

docs/configuration.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ jupyterlite_config = "jupyter_lite_config.json"
6767
jupyterlite_overrides = "overrides.json"
6868
```
6969

70+
# Setting default button texts for the `JupyterLite`, `NotebookLite`, and `Voici` directives
71+
72+
When using the `:new_tab:` option in the `JupyterLite`, `NotebookLite`, and `Voici` directives,
73+
the button text defaults to "Open as a notebook" and "Open with Voici", respectively.
74+
75+
You can optionally the button text on a global level for these directives by setting the
76+
following values in your `conf.py` file:
77+
78+
```python
79+
jupyterlite_button_text = "My custom JupyterLite button text"
80+
notebooklite_button_text = "My custom NotebookLite button text"
81+
voici_button_text = "My custom Voici button text"
82+
```
83+
84+
You can override this text on a per-directive basis by passing the `:button_text:` option
85+
to the directive. Note that this is compatible only if `:new_tab:` is also provided.
86+
7087
## Strip particular tagged cells from IPython Notebooks
7188

7289
When using the `NotebookLite`, `JupyterLite`, or `Voici` directives with a notebook passed to them, you can

docs/directives/jupyterlite.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ of JupyterLite.
5050
:new_tab: True
5151
```
5252

53+
When using this option, it is also possible to customise the button text, overriding the
54+
global value using an additional `:button_text:` parameter:
55+
56+
```rst
57+
.. jupyterlite:: my_notebook.ipynb
58+
:new_tab: True
59+
:button_text: My custom JupyterLite button text
60+
```
61+
62+
```{eval-rst}
63+
.. jupyterlite:: my_notebook.ipynb
64+
:new_tab: True
65+
:button_text: My custom JupyterLite button text
66+
```
67+
68+
## Search parameters
69+
5370
The directive `search_params` allows to transfer some search parameters from the documentation URL to the Jupyterlite URL.\
5471
Jupyterlite will then be able to fetch these parameters from its own URL.\
5572
For example `:search_params: ["param1", "param2"]` will transfer the parameters *param1* and *param2*.

docs/directives/notebooklite.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,18 @@ Lab interface.
4545
.. notebooklite:: my_notebook.ipynb
4646
:new_tab: True
4747
```
48+
49+
When using this option, it is also possible to customise the button text, overriding the
50+
global value using an additional `:button_text:` parameter:
51+
52+
```rst
53+
.. notebooklite:: my_notebook.ipynb
54+
:new_tab: True
55+
:button_text: My custom NotebookLite button text
56+
```
57+
58+
```{eval-rst}
59+
.. notebooklite:: my_notebook.ipynb
60+
:new_tab: True
61+
:button_text: My custom NotebookLite button text
62+
```

docs/directives/voici.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ the notebook in a new browser tab, instead of in the current page.
4141
:new_tab: True
4242
```
4343

44+
When using this option, it is also possible to customise the button text, overriding the
45+
global value using an additional `:button_text:` parameter:
46+
47+
```rst
48+
.. voici:: my_notebook.ipynb
49+
:new_tab: True
50+
:button_text: My custom Voici button text
51+
```
52+
53+
```{eval-rst}
54+
.. voici:: my_notebook.ipynb
55+
:new_tab: True
56+
:button_text: My custom Voici button text
57+
```

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 44 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,24 @@ 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(
468+
"'button_text' is only valid if 'new_tab' is True. To modify the prompt text, use 'prompt' and 'prompt_color'."
469+
)
470+
443471
if new_tab:
444472
return [
445473
self.newtab_cls(
@@ -451,6 +479,7 @@ def run(self):
451479
prompt_color=prompt_color,
452480
search_params=search_params,
453481
lite_options=self.options,
482+
button_text=button_text,
454483
)
455484
]
456485

@@ -482,6 +511,9 @@ class BaseJupyterViewDirective(_LiteDirective):
482511
"prompt_color": directives.unchanged,
483512
"search_params": directives.unchanged,
484513
"new_tab": directives.unchanged,
514+
# "button_text" below is valid only if "new_tab" is True, otherwise
515+
# we have "prompt" and "prompt_color" as options already.
516+
"button_text": directives.unchanged,
485517
}
486518

487519

@@ -895,6 +927,16 @@ def setup(app):
895927
rebuild="html",
896928
)
897929

930+
# Allow customising the button text for each directive (only when "new_tab" is True,
931+
# error otherwise)
932+
app.add_config_value(
933+
"jupyterlite_button_text", "Open as a notebook", rebuild="html"
934+
)
935+
app.add_config_value(
936+
"notebooklite_button_text", "Open as a notebook", rebuild="html"
937+
)
938+
app.add_config_value("voici_button_text", "Open with Voici", rebuild="html")
939+
898940
# Initialize NotebookLite and JupyterLite directives
899941
app.add_node(
900942
NotebookLiteIframe,

0 commit comments

Comments
 (0)