From 1b3b20bdd0e06935a0d13c0495243a1bff729976 Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Thu, 2 Jan 2025 03:09:37 +0530 Subject: [PATCH 1/8] Pin to `jupyterlite-core==0.5.0b0` --- pyproject.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4473438..150c644 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,9 @@ dependencies = [ "docutils", "jupyter_server", "jupyterlab_server", - "jupyterlite-core >=0.2,<0.6", + # temporary: for trying out no-code-run REPL. + # wait for 0.5.0 stable release + "jupyterlite-core==0.5.0b0", "jupytext", "nbformat", "sphinx>=4", From 9265095b97814ffb9c01a7433a0e465b90d13dba Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Thu, 2 Jan 2025 03:09:04 +0530 Subject: [PATCH 2/8] Allow enabling/disabling execution of REPL code --- jupyterlite_sphinx/jupyterlite_sphinx.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/jupyterlite_sphinx/jupyterlite_sphinx.py b/jupyterlite_sphinx/jupyterlite_sphinx.py index 36bddec..8ed71a2 100644 --- a/jupyterlite_sphinx/jupyterlite_sphinx.py +++ b/jupyterlite_sphinx/jupyterlite_sphinx.py @@ -269,6 +269,9 @@ def __init__( code = "\n".join(code_lines) lite_options["code"] = code + if "execute" in lite_options and lite_options["execute"] == "0": + lite_options["execute"] = "0" + app_path = self.lite_app if notebook is not None: lite_options["path"] = notebook @@ -401,6 +404,7 @@ class RepliteDirective(SphinxDirective): "width": directives.unchanged, "height": directives.unchanged, "kernel": directives.unchanged, + "execute": directives.unchanged, "toolbar": directives.unchanged, "theme": directives.unchanged, "prompt": directives.unchanged, @@ -421,6 +425,17 @@ def run(self): new_tab = self.options.pop("new_tab", False) + # We first check the global config, and then the per-directive + # option. It defaults to true for backwards compatibility. + execute = self.options.pop("execute", None) + if execute is None: + execute = str(self.env.config.replite_auto_execute).lower() + else: + execute = execute.lower() + + if execute == "false": + self.options["execute"] = "0" + content = self.content button_text = None @@ -1155,6 +1170,7 @@ def setup(app): man=(skip, None), ) app.add_directive("replite", RepliteDirective) + app.add_config_value("replite_auto_execute", True, rebuild="html") # Initialize Voici directive and tabbed interface app.add_node( From 83fb3778fc185cc6d8488845fc938e09a0b0cea8 Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Thu, 2 Jan 2025 03:12:29 +0530 Subject: [PATCH 3/8] Add docs for `:execute:` and `replite_auto_execute` --- docs/configuration.md | 6 ++++++ docs/directives/replite.md | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index fb73d52..a03f28c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -85,6 +85,12 @@ voici_new_tab_button_text = "My custom Voici button text" You can override this text on a per-directive basis by passing the `:new_tab_button_text:` option to the directive. Note that this is compatible only if `:new_tab:` is also provided. +## REPL code auto-execution with the `Replite` directive + +It is possible to control whether code snippets in REPL environments automatically executeswhen loaded. +For this, you may set `replite_auto_execute = False` globally in `conf.py` with (defaults to `True` if +not present), or override it on a per-directive basis with `:execute: true` or `:execute: false`. + ## Strip particular tagged cells from IPython Notebooks When using the `NotebookLite`, `JupyterLite`, or `Voici` directives with a notebook passed to them, you can diff --git a/docs/directives/replite.md b/docs/directives/replite.md index 1bbd634..69d2685 100644 --- a/docs/directives/replite.md +++ b/docs/directives/replite.md @@ -110,3 +110,44 @@ global value using an additional `:new_tab_button_text:` parameter: ax.plot(x, y) plt.show() ``` + +It is also possible to disable the execution of the code in the Replite console by setting the `:execute:` option to `False`: + +```rst +.. replite:: + :kernel: xeus-python + :new_tab: True # False works too + :new_tab_button_text: Open REPL with the code execution disabled + :execute: False + + import matplotlib.pyplot as plt + import numpy as np + + x = np.linspace(0, 2 * np.pi, 200) + y = np.sin(x) + + fig, ax = plt.subplots() + ax.plot(x, y) + plt.show() +``` + +```{eval-rst} +.. replite:: + :kernel: xeus-python + :new_tab: True # False works too + :new_tab_button_text: Open REPL with the code execution disabled + :execute: False + + import matplotlib.pyplot as plt + import numpy as np + + x = np.linspace(0, 2 * np.pi, 200) + y = np.sin(x) + + fig, ax = plt.subplots() + ax.plot(x, y) + plt.show() +``` + +The behaviour can also be [configured globally](../configuration.md#replite-auto-execution-with-the-replite-directive) +and then overridden in individual directives. From 15b790719f74471b565db3e1616f318788e72a5b Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:16:21 +0530 Subject: [PATCH 4/8] Missing space Co-authored-by: M Bussonnier --- docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index a03f28c..0d88a7e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -87,7 +87,7 @@ to the directive. Note that this is compatible only if `:new_tab:` is also provi ## REPL code auto-execution with the `Replite` directive -It is possible to control whether code snippets in REPL environments automatically executeswhen loaded. +It is possible to control whether code snippets in REPL environments automatically executes when loaded. For this, you may set `replite_auto_execute = False` globally in `conf.py` with (defaults to `True` if not present), or override it on a per-directive basis with `:execute: true` or `:execute: false`. From 1f3ae470d5d6aff2f320edd5805fb912fcc6996c Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:18:09 +0530 Subject: [PATCH 5/8] Revert "Pin to `jupyterlite-core==0.5.0b0`" This reverts commit 1b3b20bdd0e06935a0d13c0495243a1bff729976. --- pyproject.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 150c644..4473438 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,9 +16,7 @@ dependencies = [ "docutils", "jupyter_server", "jupyterlab_server", - # temporary: for trying out no-code-run REPL. - # wait for 0.5.0 stable release - "jupyterlite-core==0.5.0b0", + "jupyterlite-core >=0.2,<0.6", "jupytext", "nbformat", "sphinx>=4", From efe537496b2303d36aba58040f5e0bd1899c2f0c Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Fri, 10 Jan 2025 01:03:12 +0530 Subject: [PATCH 6/8] Consistent boolean options --- docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0d88a7e..7ff1b1d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -89,7 +89,7 @@ to the directive. Note that this is compatible only if `:new_tab:` is also provi It is possible to control whether code snippets in REPL environments automatically executes when loaded. For this, you may set `replite_auto_execute = False` globally in `conf.py` with (defaults to `True` if -not present), or override it on a per-directive basis with `:execute: true` or `:execute: false`. +not present), or override it on a per-directive basis with `:execute: True` or `:execute: False`. ## Strip particular tagged cells from IPython Notebooks From 0d4bc26ee5d2f3e0cd88ae02bd7026be1dda4dac Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Fri, 10 Jan 2025 01:04:31 +0530 Subject: [PATCH 7/8] Guard `execute:` option usage, note compatibility --- docs/directives/replite.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/directives/replite.md b/docs/directives/replite.md index 69d2685..26f115f 100644 --- a/docs/directives/replite.md +++ b/docs/directives/replite.md @@ -111,7 +111,14 @@ global value using an additional `:new_tab_button_text:` parameter: plt.show() ``` -It is also possible to disable the execution of the code in the Replite console by setting the `:execute:` option to `False`: +````{tip} + + With `jupyterlite-core` **versions 0.5.0 and later**, it is also possible to disable the execution of + the code in the Replite console by setting the `:execute:` option to `False`. This option defaults to `True`, + and setting it has no effect in versions prior to 0.5.0. + + The behaviour can also be [configured globally](../configuration.md#replite-auto-execution-with-the-replite-directive) + and then overridden in individual directives as needed. ```rst .. replite:: @@ -149,5 +156,4 @@ It is also possible to disable the execution of the code in the Replite console plt.show() ``` -The behaviour can also be [configured globally](../configuration.md#replite-auto-execution-with-the-replite-directive) -and then overridden in individual directives. +```` From 00481a431821674197a4f9ea734d8a4981b0e9d8 Mon Sep 17 00:00:00 2001 From: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> Date: Fri, 10 Jan 2025 02:00:38 +0530 Subject: [PATCH 8/8] Simplify control flow --- jupyterlite_sphinx/jupyterlite_sphinx.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/jupyterlite_sphinx/jupyterlite_sphinx.py b/jupyterlite_sphinx/jupyterlite_sphinx.py index 8ed71a2..2ae89f3 100644 --- a/jupyterlite_sphinx/jupyterlite_sphinx.py +++ b/jupyterlite_sphinx/jupyterlite_sphinx.py @@ -423,17 +423,14 @@ def run(self): search_params = search_params_parser(self.options.pop("search_params", False)) - new_tab = self.options.pop("new_tab", False) - # We first check the global config, and then the per-directive - # option. It defaults to true for backwards compatibility. - execute = self.options.pop("execute", None) - if execute is None: - execute = str(self.env.config.replite_auto_execute).lower() - else: - execute = execute.lower() + # option. It defaults to True for backwards compatibility. + execute = self.options.pop("execute", str(self.env.config.replite_auto_execute)) - if execute == "false": + if execute not in ("True", "False"): + raise ValueError("The :execute: option must be either True or False") + + if execute == "False": self.options["execute"] = "0" content = self.content @@ -445,6 +442,8 @@ def run(self): os.path.dirname(self.get_source_info()[0]), ) + new_tab = self.options.pop("new_tab", False) + if new_tab: directive_button_text = self.options.pop("new_tab_button_text", None) if directive_button_text is not None: