Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: skip stopped cells when running as a script (#4965)

Akshay Agrawal039a5b5d1025
diff --git a/marimo/_runtime/app/script_runner.py b/marimo/_runtime/app/script_runner.py
index 0456b2f133..f42841bcba 100644
--- a/marimo/_runtime/app/script_runner.py
+++ b/marimo/_runtime/app/script_runner.py
@@ -44,7 +44,7 @@ class AppScriptRunner:
) -> None:
self.app = app
self.filename = filename
- self.cells_cancelled: dict[CellId_t, set[CellId_t]] = {}
+ self.cells_cancelled: set[CellId_t] = set()
self._glbls = glbls if glbls else {}
self.cells_to_run = [
cid
@@ -55,15 +55,16 @@ class AppScriptRunner:
self._executor = get_executor(ExecutionConfig())
def _cancel(self, cell_id: CellId_t) -> None:
- self.cells_cancelled[cell_id] = set(
+ cancelled = set(
cid
for cid in dataflow.transitive_closure(
self.app.graph, set([cell_id])
)
if cid in self.cells_to_run
)
- for cid in self.cells_cancelled[cell_id]:
+ for cid in cancelled:
self.app.graph.cells[cid].set_run_result_status("cancelled")
+ self.cells_cancelled |= cancelled
def _run_synchronous(
self,
@@ -79,6 +80,8 @@ class AppScriptRunner:
outputs: dict[CellId_t, Any] = {}
while self.cells_to_run:
cid = self.cells_to_run.pop(0)
+ if cid in self.cells_cancelled:
+ continue
# Set up has already run in this case.
if cid == CellId_t(SETUP_CELL_NAME):
for hook in post_execute_hooks:
@@ -119,6 +122,9 @@ class AppScriptRunner:
while self.cells_to_run:
cid = self.cells_to_run.pop(0)
+ if cid in self.cells_cancelled:
+ continue
+
if cid == CellId_t(SETUP_CELL_NAME):
for hook in post_execute_hooks:
hook()
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index db0c0e5be1..c14877c54b 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -492,6 +492,73 @@ class TestApp:
assert "x" not in defs
assert defs["y"] == 1
+ @staticmethod
+ def test_run_mo_stop_descendant() -> None:
+ app = App()
+
+ @app.cell
+ def _() -> Any:
+ import marimo as mo
+ return (mo,)
+
+ @app.cell
+ def _(mo) -> tuple[int]:
+ mo.stop(True)
+ x = 0
+ return (x,)
+
+ @app.cell
+ def _(x) -> tuple[int]:
+ y = 1
+ x
+ return
+
+ _, defs = app.run()
+ assert "x" not in defs
+ assert "y" not in defs
+
+ @staticmethod
+ def test_run_mo_stop_descendant_multiple() -> None:
+ app = App()
+
+ @app.cell
+ def _() -> Any:
+ import marimo as mo
+ return (mo,)
+
+ @app.cell
+ def _(mo) -> tuple[int]:
+ mo.stop(True)
+ x = 0
+ return (x,)
+
+ @app.cell
+ def _(mo) -> tuple[int]:
+ mo.stop(True)
+ y = 0
+ return (y,)
+
+
+ @app.cell
+ def _(x) -> tuple[int]:
+ x
+ a = 0
+ return
+
+ @app.cell
+ def _(y) -> tuple[int]:
+ y
+ b = 0
+ return
+
+
+ _, defs = app.run()
+ assert "x" not in defs
+ assert "y" not in defs
+ assert "a" not in defs
+ assert "b" not in defs
+
+
@staticmethod
def test_run_mo_stop_async() -> None:
app = App()
@@ -516,6 +583,31 @@ class TestApp:
assert "x" not in defs
assert defs["y"] == 1
+ @staticmethod
+ def test_run_mo_stop_descendant_async() -> None:
+ app = App()
+
+ @app.cell
+ def _() -> Any:
+ import marimo as mo
+ return (mo,)
+
+ @app.cell
+ def _(mo) -> tuple[int]:
+ mo.stop(True)
+ x = 0
+ return (x,)
+
+ @app.cell
+ async def _(x) -> tuple[int]:
+ y = 1
+ x
+ return
+
+ _, defs = app.run()
+ assert "x" not in defs
+ assert "y" not in defs
+
@pytest.mark.skipif(
condition=not DependencyManager.matplotlib.has(),