Skip to content

Commit 0a0d5a0

Browse files
authored
Merge pull request #245 from agriyakhetarpal/feat/no-execute-replite-kernel
Allow enabling/disabling REPL code execution in the `Replite` directive
2 parents 02f9937 + 00481a4 commit 0a0d5a0

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

docs/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ voici_new_tab_button_text = "My custom Voici button text"
8585
You can override this text on a per-directive basis by passing the `:new_tab_button_text:` option
8686
to the directive. Note that this is compatible only if `:new_tab:` is also provided.
8787

88+
## REPL code auto-execution with the `Replite` directive
89+
90+
It is possible to control whether code snippets in REPL environments automatically executes when loaded.
91+
For this, you may set `replite_auto_execute = False` globally in `conf.py` with (defaults to `True` if
92+
not present), or override it on a per-directive basis with `:execute: True` or `:execute: False`.
93+
8894
## Strip particular tagged cells from IPython Notebooks
8995

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

docs/directives/replite.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,50 @@ global value using an additional `:new_tab_button_text:` parameter:
110110
ax.plot(x, y)
111111
plt.show()
112112
```
113+
114+
````{tip}
115+
116+
With `jupyterlite-core` **versions 0.5.0 and later**, it is also possible to disable the execution of
117+
the code in the Replite console by setting the `:execute:` option to `False`. This option defaults to `True`,
118+
and setting it has no effect in versions prior to 0.5.0.
119+
120+
The behaviour can also be [configured globally](../configuration.md#replite-auto-execution-with-the-replite-directive)
121+
and then overridden in individual directives as needed.
122+
123+
```rst
124+
.. replite::
125+
:kernel: xeus-python
126+
:new_tab: True # False works too
127+
:new_tab_button_text: Open REPL with the code execution disabled
128+
:execute: False
129+
130+
import matplotlib.pyplot as plt
131+
import numpy as np
132+
133+
x = np.linspace(0, 2 * np.pi, 200)
134+
y = np.sin(x)
135+
136+
fig, ax = plt.subplots()
137+
ax.plot(x, y)
138+
plt.show()
139+
```
140+
141+
```{eval-rst}
142+
.. replite::
143+
:kernel: xeus-python
144+
:new_tab: True # False works too
145+
:new_tab_button_text: Open REPL with the code execution disabled
146+
:execute: False
147+
148+
import matplotlib.pyplot as plt
149+
import numpy as np
150+
151+
x = np.linspace(0, 2 * np.pi, 200)
152+
y = np.sin(x)
153+
154+
fig, ax = plt.subplots()
155+
ax.plot(x, y)
156+
plt.show()
157+
```
158+
159+
````

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ def __init__(
269269
code = "\n".join(code_lines)
270270
lite_options["code"] = code
271271

272+
if "execute" in lite_options and lite_options["execute"] == "0":
273+
lite_options["execute"] = "0"
274+
272275
app_path = self.lite_app
273276
if notebook is not None:
274277
lite_options["path"] = notebook
@@ -401,6 +404,7 @@ class RepliteDirective(SphinxDirective):
401404
"width": directives.unchanged,
402405
"height": directives.unchanged,
403406
"kernel": directives.unchanged,
407+
"execute": directives.unchanged,
404408
"toolbar": directives.unchanged,
405409
"theme": directives.unchanged,
406410
"prompt": directives.unchanged,
@@ -419,7 +423,15 @@ def run(self):
419423

420424
search_params = search_params_parser(self.options.pop("search_params", False))
421425

422-
new_tab = self.options.pop("new_tab", False)
426+
# We first check the global config, and then the per-directive
427+
# option. It defaults to True for backwards compatibility.
428+
execute = self.options.pop("execute", str(self.env.config.replite_auto_execute))
429+
430+
if execute not in ("True", "False"):
431+
raise ValueError("The :execute: option must be either True or False")
432+
433+
if execute == "False":
434+
self.options["execute"] = "0"
423435

424436
content = self.content
425437

@@ -430,6 +442,8 @@ def run(self):
430442
os.path.dirname(self.get_source_info()[0]),
431443
)
432444

445+
new_tab = self.options.pop("new_tab", False)
446+
433447
if new_tab:
434448
directive_button_text = self.options.pop("new_tab_button_text", None)
435449
if directive_button_text is not None:
@@ -1155,6 +1169,7 @@ def setup(app):
11551169
man=(skip, None),
11561170
)
11571171
app.add_directive("replite", RepliteDirective)
1172+
app.add_config_value("replite_auto_execute", True, rebuild="html")
11581173

11591174
# Initialize Voici directive and tabbed interface
11601175
app.add_node(

0 commit comments

Comments
 (0)