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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: status transitions should not be repeated (#1671)
Akshay Agrawal8b98402ed799
diff --git a/marimo/_ast/cell.py b/marimo/_ast/cell.pyindex 5a3d50d964..77faf9574e 100644--- a/marimo/_ast/cell.py+++ b/marimo/_ast/cell.py@@ -172,6 +172,7 @@ class CellImpl:def set_status(self, status: CellStatusType, stream: Stream | None = None) -> None:+ """Set execution status and broadcast to frontends."""from marimo._messaging.ops import CellOpfrom marimo._runtime.context import (ContextNotInitializedError,diff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.pyindex 267818f365..9be39c397a 100644--- a/marimo/_messaging/ops.py+++ b/marimo/_messaging/ops.py@@ -238,7 +238,6 @@ class CellOp(Op):data: Sequence[Error],clear_console: bool,cell_id: CellId_t,- status: Optional[CellStatusType],) -> None:console: Optional[list[CellOutput]] = [] if clear_console else NoneCellOp(@@ -249,7 +248,7 @@ class CellOp(Op):data=data,),console=console,- status=status,+ status=None,).broadcast()@staticmethoddiff --git a/marimo/_runtime/runner/execution_hooks.py b/marimo/_runtime/runner/execution_hooks.pydeleted file mode 100644index 0df081ca51..0000000000--- a/marimo/_runtime/runner/execution_hooks.py+++ /dev/null@@ -1,4 +0,0 @@-# Copyright 2024 Marimo. All rights reserved.-from marimo import _loggers--LOGGER = _loggers.marimo_logger()diff --git a/marimo/_runtime/runner/hooks_on_finish.py b/marimo/_runtime/runner/hooks_on_finish.pyindex fcab77359a..dfb524fc42 100644--- a/marimo/_runtime/runner/hooks_on_finish.py+++ b/marimo/_runtime/runner/hooks_on_finish.py@@ -28,7 +28,6 @@ def _send_interrupt_errors(runner: cell_runner.Runner) -> None:# reflect a previous run and should be clearedclear_console=True,cell_id=cid,- status="idle",)@@ -36,7 +35,12 @@ def _send_cancellation_errors(runner: cell_runner.Runner) -> None:for raising_cell in runner.cells_cancelled:for cid in runner.cells_cancelled[raising_cell]:# `cid` was not run- runner.graph.cells[cid].set_status("idle")+ cell = runner.graph.cells[cid]+ if cell.status != "idle":+ # the cell raising an exception will already be+ # idle, but its descendants won't be.+ cell.set_status("idle")+exception = runner.exceptions[raising_cell]data: Errorif isinstance(exception, MarimoStopError):@@ -74,7 +78,6 @@ def _send_cancellation_errors(runner: cell_runner.Runner) -> None:# reflect a previous run and should be clearedclear_console=True,cell_id=cid,- status="idle",)diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.pyindex fd544a963b..5effa5b3e9 100644--- a/marimo/_runtime/runner/hooks_post_execution.py+++ b/marimo/_runtime/runner/hooks_post_execution.py@@ -128,7 +128,7 @@ def _broadcast_outputs(mimetype=formatted_output.mimetype,data=formatted_output.data,cell_id=cell.cell_id,- status=cell.status,+ status=None,)elif isinstance(run_result.exception, MarimoStrictExecutionError):LOGGER.debug("Cell %s raised a strict error", cell.cell_id)@@ -137,7 +137,6 @@ def _broadcast_outputs(data=[run_result.output],clear_console=True,cell_id=cell.cell_id,- status=cell.status,)elif isinstance(run_result.exception, MarimoInterrupt):LOGGER.debug("Cell %s was interrupted", cell.cell_id)@@ -147,7 +146,6 @@ def _broadcast_outputs(data=[MarimoInterruptionError()],clear_console=False,cell_id=cell.cell_id,- status=cell.status,)elif run_result.exception is not None:LOGGER.debug(@@ -176,7 +174,6 @@ def _broadcast_outputs(],clear_console=False,cell_id=cell.cell_id,- status=cell.status,)diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex e4dfaf8ce5..21875fa476 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -838,7 +838,6 @@ class Kernel:data=self.errors[cid],clear_console=True,cell_id=cid,- status=None,)Variables(diff --git a/tests/_plugins/ui/_impl/test_run_button.py b/tests/_plugins/ui/_impl/test_run_button.pynew file mode 100644index 0000000000..f8570541f8--- /dev/null+++ b/tests/_plugins/ui/_impl/test_run_button.py@@ -0,0 +1,52 @@+from marimo._plugins import ui+from marimo._runtime.requests import SetUIElementValueRequest+from marimo._runtime.runtime import Kernel+from tests.conftest import ExecReqProvider+++def test_run_button_initial_value():+ run_button = ui.run_button()+ assert not run_button.value+++async def test_run_button_set_to_true_on_click(+ any_kernel: Kernel, exec_req: ExecReqProvider+):+ k = any_kernel+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get("b = mo.ui.run_button()"),+ er := exec_req.get(+ """+ if b.value:+ x = 1+ else:+ x = 0+ """+ ),+ ]+ )++ run_button = k.globals["b"]+ assert not run_button.value++ await k.set_ui_element_value(+ SetUIElementValueRequest([run_button._id], [1])+ )++ if not k.lazy():+ assert k.globals["x"] == 1+ assert not run_button.value+ else:+ # value is updated ...+ assert run_button.value+ # ... but we haven't run its descendants yet+ assert k.globals["x"] == 0++ await k.run([er])++ assert k.globals["x"] == 1+ # in lazy kernels, run button's value is not set to False, since+ # we don't know when its descendants have run+ assert run_button.valuediff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex f6764b9179..7a92c317b1 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -13,6 +13,7 @@ from marimo._messaging.errors import (Error,MultipleDefinitionError,)+from marimo._messaging.ops import CellOpfrom marimo._messaging.types import NoopStreamfrom marimo._plugins.ui._core.ids import IDProviderfrom marimo._plugins.ui._core.ui_element import UIElement@@ -26,7 +27,8 @@ from marimo._runtime.requests import (SetUIElementValueRequest,)from marimo._runtime.runtime import Kernel-from tests.conftest import ExecReqProvider+from marimo._utils.parse_dataclass import parse_raw+from tests.conftest import ExecReqProvider, MockedKernelif TYPE_CHECKING:import pathlib@@ -887,12 +889,14 @@ class TestStrictExecution:k = strict_kernelawait k.run([- exec_req.get("""+ exec_req.get(+ """Y = 1_y = 1def f(x):return x + _y- """),+ """+ ),exec_req.get("""_x = 1@@ -918,12 +922,14 @@ class TestStrictExecution:k = strict_kernelawait k.run([- exec_req.get("""+ exec_req.get(+ """class namespace:...X = namespace()X.count = 1- """),+ """+ ),exec_req.get("""X.count += 1@@ -953,14 +959,16 @@ class TestStrictExecution:k = strict_kernelawait k.run([- exec_req.get("""+ exec_req.get(+ """import marimo as moclass namespace:...X = namespace()X.count = 1X = mo._runtime.copy.zero_copy(X)- """),+ """+ ),exec_req.get("""mo._runtime.copy.unwrap_copy(X).count += 1@@ -1620,3 +1628,178 @@ class TestAsyncIO:)assert not k.errorsassert k.globals["res"] == "done"+++class TestStateTransitions:+ async def test_statuses_not_repeated_ok_run(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ exec_req.get("x = 0"),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 1++ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 1++ async def test_statuses_not_repeated_on_stop(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ exec_req.get("import marimo as mo; mo.stop(True)"),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 1++ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 1++ async def test_statuses_not_repeated_on_interruption(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ exec_req.get(+ "from marimo._runtime.control_flow import MarimoInterrupt; raise MarimoInterrupt()" # noqa: E501+ ),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 1++ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 1++ async def test_statuses_not_repeated_on_exception(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ exec_req.get("raise ValueError"),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 1++ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 1++ async def test_descendant_status_reset_to_idle_on_error(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ er_1 := exec_req.get("x = 0; raise ValueError"),+ er_2 := exec_req.get("x"),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ # er_1 and er_2+ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 2++ # only er_1 runs+ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ # er_1 and er_2+ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 2++ assert k.graph.cells[er_1.cell_id].status == "idle"+ assert k.graph.cells[er_2.cell_id].status == "idle"++ async def test_descendant_status_reset_to_idle_on_interrupt(+ self, mocked_kernel: MockedKernel, exec_req: ExecReqProvider+ ) -> None:+ k = mocked_kernel.k+ await k.run(+ [+ er_1 := exec_req.get(+ """+ from marimo._runtime.control_flow import MarimoInterrupt++ x = 0+ raise MarimoInterrupt+ """+ ),+ er_2 := exec_req.get("x"),+ ]+ )++ cell_ops = [+ parse_raw(op_data, CellOp)+ for op_name, op_data in mocked_kernel.stream.messages+ if op_name == "cell-op"+ ]++ # er_1 and er_2+ n_queued = sum([1 for op in cell_ops if op.status == "queued"])+ assert n_queued == 2++ # only er_1 runs+ n_running = sum([1 for op in cell_ops if op.status == "running"])+ assert n_running == 1++ # er_1 and er_2+ n_idle = sum([1 for op in cell_ops if op.status == "idle"])+ assert n_idle == 2++ assert k.graph.cells[er_1.cell_id].status == "idle"+ assert k.graph.cells[er_2.cell_id].status == "idle"