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: include model messages in SessionView for refresh (#8150)
Myles Scolnickdbe0b7790d85
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.pyindex efcd9a460f..b3b0eff2c1 100644--- a/marimo/_messaging/notification.py+++ b/marimo/_messaging/notification.py@@ -31,7 +31,7 @@ from marimo._plugins.core.web_component import JSONTypefrom marimo._runtime.layout.layout import LayoutConfigfrom marimo._secrets.models import SecretKeysWithProviderfrom marimo._sql.parse import SqlCatalogCheckResult, SqlParseResult-from marimo._types.ids import CellId_t, RequestId, WidgetModelId+from marimo._types.ids import CellId_t, RequestId, UIElementId, WidgetModelIdfrom marimo._utils.msgspec_basestruct import BaseStructfrom marimo._utils.platform import is_pyodide, is_windows@@ -144,15 +144,13 @@ class UIElementMessageNotification("""Sends a message to a UI element/widget.Attributes:- ui_element: UI element identifier (legacy).- model_id: Widget model ID (newer architecture).+ ui_element: UI element identifier.message: Message payload as dictionary.buffers: Optional binary buffers for large data."""name: ClassVar[str] = "send-ui-element-message"- ui_element: Optional[str]- model_id: Optional[WidgetModelId]+ ui_element: UIElementIdmessage: dict[str, Any]buffers: Optional[list[bytes]] = Nonediff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.pyindex 7ea85a71a5..54b32c202b 100644--- a/marimo/_plugins/ui/_core/ui_element.py+++ b/marimo/_plugins/ui/_core/ui_element.py@@ -441,7 +441,6 @@ class UIElement(Html, Generic[S, T]):broadcast_notification(UIElementMessageNotification(ui_element=self._id,- model_id=None,message=message,buffers=list(buffers or []),),diff --git a/marimo/_session/state/session_view.py b/marimo/_session/state/session_view.pyindex e0e7d0f909..eb51a4b7be 100644--- a/marimo/_session/state/session_view.py+++ b/marimo/_session/state/session_view.py@@ -15,6 +15,7 @@ from marimo._messaging.notification import (DataSourceConnectionsNotification,InstallingPackageAlertNotification,InterruptedNotification,+ ModelLifecycleNotification,NotificationMessage,SQLTableListPreviewNotification,SQLTablePreviewNotification,@@ -41,7 +42,7 @@ from marimo._sql.connection_utils import (update_table_list_in_connection,)from marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINE-from marimo._types.ids import CellId_t, WidgetModelId+from marimo._types.ids import CellId_t, UIElementId, WidgetModelIdfrom marimo._utils.lists import as_listLOGGER = _loggers.marimo_logger()@@ -111,7 +112,11 @@ class SessionView:self.stale_code: Optional[UpdateCellCodesNotification] = None# Model messagesself.model_messages: dict[- WidgetModelId, list[UIElementMessageNotification]+ WidgetModelId, list[ModelLifecycleNotification]+ ] = {}+ # UI element messages+ self.ui_element_messages: dict[+ UIElementId, list[UIElementMessageNotification]] = {}# Startup logs for startup command - only one at a time@@ -299,14 +304,24 @@ class SessionView:self.stale_code = notificationelif isinstance(notification, UIElementMessageNotification):- if notification.model_id is None:- return- messages = self.model_messages.get(notification.model_id, [])- messages.append(notification)# TODO(perf): Consider merging consecutive 'update' messages# to reduce replay size. Could keep only the latest state for# each model_id instead of the full message history.- self.model_messages[notification.model_id] = messages+ # TODO: cleanup old UI messages+ ui_element_id = notification.ui_element+ if ui_element_id not in self.ui_element_messages:+ self.ui_element_messages[ui_element_id] = []+ self.ui_element_messages[ui_element_id].append(notification)++ elif isinstance(notification, ModelLifecycleNotification):+ model_id = notification.model_id+ if model_id not in self.model_messages:+ self.model_messages[model_id] = []+ # TODO(perf): Consider merging consecutive 'update' messages+ # to reduce replay size. Could keep only the latest state for+ # each model_id instead of the full message history.+ # TODO: cleanup old UI messages+ self.model_messages[model_id].append(notification)elif isinstance(notification, StartupLogsNotification):prev = self.startup_logs.content if self.startup_logs else ""@@ -446,8 +461,12 @@ class SessionView:# Model messages must come before cell notifications to ensure# the model exists before the view tries to use it.if self.model_messages:- for messages in self.model_messages.values():- all_notifications.extend(messages)+ for model_messages in self.model_messages.values():+ all_notifications.extend(model_messages)++ if self.ui_element_messages:+ for ui_messages in self.ui_element_messages.values():+ all_notifications.extend(ui_messages)all_notifications.extend(self.cell_notifications.values())if self.stale_code:diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yamlindex ed58d6cb6e..02168e8889 100644--- a/packages/openapi/api.yaml+++ b/packages/openapi/api.yaml@@ -4313,9 +4313,8 @@ components:type: objectUIElementMessageNotification:description: "Sends a message to a UI element/widget.\n\n Attributes:\n \- \ ui_element: UI element identifier (legacy).\n model_id: Widget\- \ model ID (newer architecture).\n message: Message payload as dictionary.\n\- \ buffers: Optional binary buffers for large data."+ \ ui_element: UI element identifier.\n message: Message payload\+ \ as dictionary.\n buffers: Optional binary buffers for large data."properties:buffers:anyOf:@@ -4327,21 +4326,14 @@ components:default: nullmessage:type: object- model_id:- anyOf:- - type: string- - type: 'null'op:enum:- send-ui-element-messageui_element:- anyOf:- - type: string- - type: 'null'+ type: stringrequired:- op- ui_element- - model_id- messagetitle: UIElementMessageNotificationtype: objectdiff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.tsindex ebd4b28548..fa18303d87 100644--- a/packages/openapi/src/api.ts+++ b/packages/openapi/src/api.ts@@ -5658,8 +5658,7 @@ export interface components {* @description Sends a message to a UI element/widget.** Attributes:- * ui_element: UI element identifier (legacy).- * model_id: Widget model ID (newer architecture).+ * ui_element: UI element identifier.* message: Message payload as dictionary.* buffers: Optional binary buffers for large data.*/@@ -5667,10 +5666,9 @@ export interface components {/** @default null */buffers?: string[] | null;message: Record<string, any>;- model_id: string | null;/** @enum {unknown} */op: "send-ui-element-message";- ui_element: string | null;+ ui_element: string;};/** UnknownError */UnknownError: {diff --git a/tests/_messaging/test_notifications.py b/tests/_messaging/test_notifications.pyindex 7f49aa09e5..2df14b9ad3 100644--- a/tests/_messaging/test_notifications.py+++ b/tests/_messaging/test_notifications.py@@ -118,7 +118,6 @@ def test_send_ui_element_message_broadcast() -> None:msg = UIElementMessageNotification(ui_element="test_element",- model_id=None,message={"action": "update", "value": 42},buffers=[b"buffer1", b"buffer2"],)@@ -130,7 +129,6 @@ def test_send_ui_element_message_broadcast() -> None:assert stream.operations[0] == {"op": "send-ui-element-message","ui_element": "test_element",- "model_id": None,"message": {"action": "update", "value": 42},"buffers": ["YnVmZmVyMQ==",diff --git a/tests/_session/state/test_session_view.py b/tests/_session/state/test_session_view.pyindex b32de6dd55..33c3b88383 100644--- a/tests/_session/state/test_session_view.py+++ b/tests/_session/state/test_session_view.py@@ -20,11 +20,12 @@ from marimo._messaging.notification import (DatasetsNotification,DataSourceConnectionsNotification,InstallingPackageAlertNotification,+ ModelLifecycleNotification,+ ModelUpdate,SQLMetadata,SQLTableListPreviewNotification,SQLTablePreviewNotification,StartupLogsNotification,- UIElementMessageNotification,UpdateCellCodesNotification,UpdateCellIdsNotification,VariableDeclarationNotification,@@ -190,40 +191,51 @@ def test_model_message_values(session_view: SessionView) -> None:model_id2 = WidgetModelId("test_model2")session_view.add_notification(- UIElementMessageNotification(+ ModelLifecycleNotification(model_id=model_id,- message={"key": "value"},- ui_element=None,+ message=ModelUpdate(+ state={"key": "value"},+ buffer_paths=[],+ buffers=[],+ ),))assert model_id in session_view.model_messages- assert session_view.model_messages[model_id][0].message == {"key": "value"}+ assert session_view.model_messages[model_id][0].message == ModelUpdate(+ state={"key": "value"}, buffer_paths=[], buffers=[]+ )# Can add to existing modelsession_view.add_notification(- UIElementMessageNotification(+ ModelLifecycleNotification(model_id=model_id,- message={"key": "new_value"},- ui_element=None,+ message=ModelUpdate(+ state={"key": "new_value"},+ buffer_paths=[],+ buffers=[],+ ),))assert len(session_view.model_messages[model_id]) == 2- assert session_view.model_messages[model_id][1].message == {- "key": "new_value"- }+ assert session_view.model_messages[model_id][1].message == ModelUpdate(+ state={"key": "new_value"}, buffer_paths=[], buffers=[]+ )# Can add multiple modelssession_view.add_notification(- UIElementMessageNotification(+ ModelLifecycleNotification(model_id=model_id2,- message={"key2": "value2"},- ui_element=None,+ message=ModelUpdate(+ state={"key2": "value2"},+ buffer_paths=[],+ buffers=[],+ ),))assert model_id2 in session_view.model_messages- assert session_view.model_messages[model_id2][0].message == {- "key2": "value2"- }+ assert session_view.model_messages[model_id2][0].message == ModelUpdate(+ state={"key2": "value2"}, buffer_paths=[], buffers=[]+ )def test_last_run_code(session_view: SessionView) -> None: