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
Allow `mo.state` setters in widget callbacks (#8244)
Trevor Manze7e1a0aecde6
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.pyindex 17c97cd02a..65b0c0cd3e 100644--- a/marimo/_runtime/runner/cell_runner.py+++ b/marimo/_runtime/runner/cell_runner.py@@ -359,6 +359,7 @@ class Runner:# by object ID (via is operator)if ref in self.glbls and self.glbls[ref] is state:cids_to_run.add(cid)+ break # cell already matched; skip remaining refsreturn cids_to_rundef pop_cell(self) -> CellId_t:diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex cb4c9f9594..cca08e4e78 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -1448,20 +1448,43 @@ class Kernel:from marimo._runtime.threads import is_marimo_threadctx = get_context()- assert ctx.execution_context is not None- setter_cell_id = ctx.execution_context.cell_id-- # When running on the main thread of execution, state updates- # are just logged in a data structure; it is the runner's- # job to process these later.- if not is_marimo_thread():- with self._state_lock:- self.state_updates[state] = setter_cell_id+ if ctx.execution_context is not None:+ setter_cell_id = ctx.execution_context.cell_id+ else:+ # Setter called outside cell execution (e.g. from a widget+ # callback triggered by a frontend message, or an async+ # task). Use a sentinel that won't match any real cell,+ # so self-loop prevention is skipped.+ setter_cell_id = CellId_t("__external__")++ # When running in a mo.Thread, eagerly process state updates.+ if is_marimo_thread():+ cells_with_stale_state = self._find_cells_for_state(+ state, setter_cell_id+ )+ self.graph.set_stale(cells_with_stale_state, prune_imports=True)+ if not self.lazy():+ self._execute_stale_cells_callback()return- # Otherwise, when running in a mo.Thread, we eagerly process- # state updates.- cells_with_stale_state = set()+ # On the main thread, queue the update for the runner.+ with self._state_lock:+ self.state_updates[state] = setter_cell_id++ # Outside cell execution (async task, widget callback), nothing+ # else will flush the queue, so enqueue a run.+ if ctx.execution_context is None and not self.lazy():+ self._execute_stale_cells_callback()++ def _find_cells_for_state(+ self, state: State[Any], setter_cell_id: CellId_t+ ) -> set[CellId_t]:+ """Find cells that should re-run due to a state update.++ Returns cell IDs whose refs include the given state object,+ excluding the setter cell (unless allow_self_loops is True).+ """+ result: set[CellId_t] = set()for cid, cell in self.graph.cells.items():# No self-loopsif cid == setter_cell_id and not state.allow_self_loops:@@ -1470,11 +1493,9 @@ class Kernel:# run this cell if any of its refs match the state object# by object ID (via is operator)if ref in self.globals and self.globals[ref] is state:- cells_with_stale_state.add(cid)- break- self.graph.set_stale(cells_with_stale_state, prune_imports=True)- if not self.lazy():- self._execute_stale_cells_callback()+ result.add(cid)+ break # cell already matched; skip remaining refs+ return result@kernel_tracer.start_as_current_span("delete_cell")async def delete_cell(self, request: DeleteCellCommand) -> None:@@ -2244,6 +2265,7 @@ class Kernel:)# If there's a ui_element_id, trigger a cell re-run+ # via the UI element path (which also processes state updates).if ui_element_id and state:await self.set_ui_element_value(UpdateUIElementCommand.from_ids_and_values(@@ -2251,6 +2273,12 @@ class Kernel:))broadcast_notification(CompletedRunNotification())+ elif self.state_updates:+ # Callbacks during message processing (e.g. widget observe+ # handlers) may have called mo.state setters. Process+ # those pending state updates now.+ await self._run_cells(set())+ broadcast_notification(CompletedRunNotification())async def handle_function_call(request: InvokeFunctionCommand) -> None:status, ret, _ = await self.function_call_request(request)diff --git a/tests/_plugins/ui/_impl/test_anywidget.py b/tests/_plugins/ui/_impl/test_anywidget.pyindex bd75cba987..b898103697 100644--- a/tests/_plugins/ui/_impl/test_anywidget.py+++ b/tests/_plugins/ui/_impl/test_anywidget.py@@ -15,8 +15,13 @@ from marimo._plugins.ui._impl.from_anywidget import (from_anywidget,get_anywidget_state,)-from marimo._runtime.commands import UpdateUIElementCommand+from marimo._runtime.commands import (+ ModelCommand,+ ModelUpdateMessage,+ UpdateUIElementCommand,+)from marimo._runtime.runtime import Kernel+from marimo._types.ids import WidgetModelIdfrom tests.conftest import ExecReqProviderHAS_DEPS = (@@ -550,3 +555,182 @@ x = as_marimo_element.count# Test KeyError propagationwith pytest.raises(KeyError):_ = wrapped["nonexistent"]++ @staticmethod+ async def test_model_message_with_observe_and_state(+ k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ """Test that mo.state setters work inside widget observe callbacks.++ Regression test: when a model update message arrives from the+ frontend, the observe callback fires outside any cell execution+ context. The __external__ sentinel ensures state setters still+ trigger downstream re-runs without causing self-loops.+ This test does NOT use mo.ui.anywidget() — the model exists+ without a view, verifying the model-to-cell mapping works+ independently of the UIElement path.+ """+ await k.run(+ [+ exec_req.get(+ """+import anywidget+import traitlets+import marimo as mo++class Counter(anywidget.AnyWidget):+ _esm = ""+ count = traitlets.Int(0).tag(sync=True)++c = Counter()+get_count, set_count = mo.state(c.count)++def _on_count_change(_):+ set_count(c.count)++c.observe(_on_count_change, names="count")+"""+ ),+ exec_req.get("result = get_count()"),+ ]+ )++ assert k.globals["result"] == 0++ widget = k.globals["c"]+ model_id = WidgetModelId(widget._model_id)++ # Simulate a model update from the frontend+ await k.handle_message(+ ModelCommand(+ model_id=model_id,+ message=ModelUpdateMessage(+ state={"count": 5},+ buffer_paths=[],+ ),+ buffers=[],+ )+ )++ # The observe callback should have fired set_count,+ # which triggers re-execution of the cell reading get_count()+ assert k.globals["result"] == 5++ @staticmethod+ async def test_nested_model_observe_and_state(+ k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ """Test observe on a child widget nested inside a parent widget.++ Models the lonboard pattern: a Map widget holds Layer children,+ and the user observes trait changes on the inner layer. The+ parent (Map) is displayed via mo.ui.anywidget, but the observe+ callback is on the child layer which has its own model.+ """+ await k.run(+ [+ exec_req.get(+ """+import anywidget+import traitlets+import marimo as mo++class Layer(anywidget.AnyWidget):+ _esm = ""+ selected_index = traitlets.Int(0).tag(sync=True)++class Map(anywidget.AnyWidget):+ _esm = ""+ layer = traitlets.Instance(Layer).tag(sync=True)++layer = Layer()+m = Map(layer=layer)+w = mo.ui.anywidget(m)++get_selected, set_selected = mo.state(layer.selected_index)+layer.observe(+ lambda _: set_selected(layer.selected_index),+ names="selected_index",+)+"""+ ),+ exec_req.get("result = get_selected()"),+ ]+ )++ assert k.globals["result"] == 0++ layer = k.globals["layer"]+ layer_model_id = WidgetModelId(layer._model_id)++ # Simulate frontend updating the child layer's trait+ await k.handle_message(+ ModelCommand(+ model_id=layer_model_id,+ message=ModelUpdateMessage(+ state={"selected_index": 42},+ buffer_paths=[],+ ),+ buffers=[],+ )+ )++ assert k.globals["result"] == 42++ @staticmethod+ async def test_observe_state_no_self_loop(+ k: Kernel, exec_req: ExecReqProvider+ ) -> None:+ """Verify that __external__ sentinel doesn't cause self-loops.++ If a cell both defines and reads a state (get_count), plus has+ an observe callback that calls the setter, the defining cell+ should NOT re-run — only downstream cells should.+ """+ await k.run(+ [+ exec_req.get(+ """+import anywidget+import traitlets+import marimo as mo++class Counter(anywidget.AnyWidget):+ _esm = ""+ count = traitlets.Int(0).tag(sync=True)++c = Counter()+get_count, set_count = mo.state(c.count)+c.observe(lambda _: set_count(c.count), names="count")++# Read the state in the SAME cell that defines it+same_cell_value = get_count()+run_count = globals().get("run_count", 0) + 1+"""+ ),+ exec_req.get("result = get_count()"),+ ]+ )++ assert k.globals["same_cell_value"] == 0+ assert k.globals["result"] == 0+ initial_run_count = k.globals["run_count"]++ widget = k.globals["c"]+ model_id = WidgetModelId(widget._model_id)++ await k.handle_message(+ ModelCommand(+ model_id=model_id,+ message=ModelUpdateMessage(+ state={"count": 7},+ buffer_paths=[],+ ),+ buffers=[],+ )+ )++ # Downstream cell should update+ assert k.globals["result"] == 7+ # The defining cell should NOT have re-run (no self-loop)+ assert k.globals["run_count"] == initial_run_countdiff --git a/tests/_runtime/test_state.py b/tests/_runtime/test_state.pyindex 2dfc5f1b9e..f8661761ab 100644--- a/tests/_runtime/test_state.py+++ b/tests/_runtime/test_state.py@@ -218,3 +218,100 @@ async def test_set_state_not_strict_copied(assert id(k.globals["a"]) == id(k.globals["state"])assert id(k.globals["b"]) == id(k.globals["set_state"])+++async def test_external_state_update(+ execution_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """State setter called outside cell execution (no execution context).++ Simulates the pattern where a widget callback or async task calls+ set_state. The __external__ sentinel should skip self-loop prevention+ and downstream cells should re-run.+ """+ k = execution_kernel+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get("state, set_state = mo.state(0)"),+ exec_req.get("x = state()"),+ ]+ )+ assert k.globals["x"] == 0++ # Call set_state outside any cell execution context,+ # like a widget callback or async task would.+ k.globals["set_state"](42)++ # _find_cells_for_state with __external__ should find the+ # cell that reads state but not loop back to any "setter" cell.+ from marimo._runtime.state import State+ from marimo._types.ids import CellId_t++ state_obj = k.globals["state"]+ assert isinstance(state_obj, State)++ affected = k._find_cells_for_state(state_obj, CellId_t("__external__"))+ # Should include the cell that reads `state` (x = state())+ assert len(affected) > 0++ # The cell that defines state should NOT be in affected+ # (it defines but doesn't read it as a ref)+ define_cell_id = list(k.graph.cells.keys())[1] # "state, set_state = ..."+ assert define_cell_id not in affected+++async def test_external_set_state_reruns_dependent_cell(+ execution_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """Calling set_state from outside a cell (async task / widget callback)+ should re-run downstream cells that read the state."""+ k = execution_kernel++ # Wire enqueue so run_stale_cells can actually flush the update+ k.enqueue_control_request = lambda _: None # type: ignore++ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get("state, set_state = mo.state(0)"),+ exec_req.get("result = state()"),+ ]+ )+ assert k.globals["result"] == 0++ # Simulate a widget callback / async task calling set_state+ k.globals["set_state"](42)++ # Flush — in production the event loop processes this automatically+ await k.run_stale_cells()++ assert k.globals["result"] == 42+++async def test_find_cells_with_multiple_refs_to_same_state(+ execution_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """Cell that references the same state via multiple names is found once."""+ k = execution_kernel+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get("state, set_state = mo.state(0)"),+ # alias creates a second ref to the same state object+ exec_req.get("alias = state; x = state() + alias()"),+ ]+ )+ assert k.globals["x"] == 0++ from marimo._runtime.state import State+ from marimo._types.ids import CellId_t++ state_obj = k.globals["state"]+ assert isinstance(state_obj, State)++ affected = k._find_cells_for_state(state_obj, CellId_t("__external__"))+ reader_cell_id = list(k.graph.cells.keys())[2]+ # Cell should appear exactly once despite two refs to the same state+ assert reader_cell_id in affected+ assert len([c for c in affected if c == reader_cell_id]) == 1