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 Panel DynamicMap and interactive streams not updating (#8538)
Trevor Manz8bb6c96eb3b8
diff --git a/frontend/src/plugins/impl/panel/PanelPlugin.tsx b/frontend/src/plugins/impl/panel/PanelPlugin.tsxindex ac3fd2f370..58350271e3 100644--- a/frontend/src/plugins/impl/panel/PanelPlugin.tsx+++ b/frontend/src/plugins/impl/panel/PanelPlugin.tsx@@ -1,8 +1,7 @@/* Copyright 2026 Marimo. All rights reserved. *//* eslint-disable @typescript-eslint/no-explicit-any */-import { useEvent } from "@dnd-kit/utilities";-import { useEffect, useRef, useState } from "react";+import { useCallback, useEffect, useRef, useState } from "react";import { z } from "zod";import { MarimoIncomingMessageEvent } from "@/core/dom/events";import {@@ -12,13 +11,14 @@ import {import { createPlugin } from "@/plugins/core/builder";import { rpc } from "@/plugins/core/rpc";import type { IPluginProps } from "@/plugins/types";+import { Logger } from "@/utils/Logger";import { EventBuffer, extractBuffers, MessageSchema } from "./utils";interface BokehDocument {create_json_patch: (events: unknown[]) => unknown;apply_json_patch: (content: unknown, buffers: ArrayBuffer[]) => void;on_change: (callback: (event: unknown) => void) => void;- _all_models: Set<string>;+ _all_models: Map<string, unknown>;}interface RenderItems {@@ -77,8 +77,8 @@ type T = Record<string, unknown>;// eslint-disable-next-line @typescript-eslint/consistent-type-definitionstype PluginFunctions = {send_to_widget: <T>(req: {- message?: any;- buffers?: any;+ message?: unknown;+ buffers?: unknown;}) => Promise<null | undefined>;};@@ -114,27 +114,52 @@ const PanelSlot = (props: Props) => {const { data, functions, host } = props;const { extension, docs_json: docsJson, render_json: renderJson } = data;const ref = useRef<HTMLDivElement>(null);- const docRef = useRef<BokehDocument | null>(null);+ const rootModelIdRef = useRef<string | null>(null);const receiverRef = useRef<InstanceType<typeof window.Bokeh.protocol.Receiver> | null>(null);const [loaded, setLoaded] = useState<boolean>(false);const [rendered, setRendered] = useState<string | null>(null);- const processEvents = useEvent(() => {- if (!eventBufferRef.current || !docRef.current) {+ // Store functions in a ref so the callback captured by EventBuffer stays current+ const functionsRef = useRef(functions);+ functionsRef.current = functions;++ const processEvents = useCallback(() => {+ if (!eventBufferRef.current) {return;}const events = eventBufferRef.current.getAndClear();- const patch = docRef.current.create_json_patch(events);- const message = {- ...window.Bokeh.protocol.Message.create("PATCH-DOC", {}, patch),- };+ if (events.length === 0) {+ return;+ }++ // Use the event's own document — it is always current, even when+ // DynamicMap/HoloViews has replaced the document after embedding.+ const firstEvent = events.at(0);+ if (!isDocumentEvent(firstEvent)) {+ return;+ }+ const doc = firstEvent.document;++ // Keep only events that belong to this document+ const sameDocEvents = events.filter(+ (ev) => isDocumentEvent(ev) && ev.document === doc,+ );++ const patch = doc.create_json_patch(sameDocEvents);+ const message = window.Bokeh.protocol.Message.create(+ "PATCH-DOC",+ {},+ patch,+ );const buffers: ArrayBuffer[] = [];message.content = extractBuffers(message.content, buffers);- functions.send_to_widget({ message, buffers });- });+ functionsRef.current.send_to_widget({ message, buffers }).catch((error) => {+ Logger.warn("Failed to send Panel event to backend", error);+ });+ }, []);const eventBufferRef = useRef<EventBuffer<unknown> | null>(new EventBuffer(processEvents),@@ -178,9 +203,8 @@ const PanelSlot = (props: Props) => {const message = MessageSchema.parse(e.detail.message);const buffers = e.detail.buffers;const receiver = receiverRef.current;- const doc = docRef.current;- if (!receiver || !doc) {+ if (!receiver) {return;}@@ -193,6 +217,11 @@ const PanelSlot = (props: Props) => {return;}+ const doc = getDoc(rootModelIdRef.current);+ if (!doc) {+ return;+ }+if (buffers && buffers.length > 0) {// Check if we already have an ArrayBufferconst buffer = buffers[0];@@ -212,7 +241,7 @@ const PanelSlot = (props: Props) => {if (commMessage != null && Object.keys(commMessage.content).length > 0) {if (commMessage.content.events !== undefined) {commMessage.content.events = commMessage.content.events.filter(- (e: any) => doc._all_models.has(e.model.id),+ (e) => e?.model?.id && doc._all_models.has(e.model.id),);}doc.apply_json_patch(commMessage.content, commMessage.buffers);@@ -243,9 +272,17 @@ const PanelSlot = (props: Props) => {}const modelId = Object.keys(renderItem.roots)[0];await window.Bokeh.embed.embed_items_notebook(docsJson, [renderItem]);- docRef.current = window.Bokeh.index.get_by_id(modelId).model.document;+ rootModelIdRef.current = modelId;+ const doc = window.Bokeh.index.get_by_id(modelId).model.document;receiverRef.current = new window.Bokeh.protocol.Receiver();- docRef.current.on_change((event) => eventBufferRef.current?.add(event));+ doc.on_change((event) => {+ // Bokeh tags server-applied patch events with sync=false via+ // model.setv({...}, {sync: false}) inside apply_json_patch.+ // Only forward user-initiated events (sync=true, the default).+ if (isSyncEvent(event) && event.sync) {+ eventBufferRef.current?.add(event);+ }+ });setRendered(docId);};@@ -263,4 +300,29 @@ const PanelSlot = (props: Props) => {);};+// Re-fetch the live document from the Bokeh index. DynamicMap/HoloViews+// may replace the document after initial embedding, so a stale ref would+// cause "Cannot create a patch using events from a different document".+function getDoc(id: string | null): BokehDocument | null {+ if (!id) {+ return null;+ }+ try {+ return window.Bokeh.index.get_by_id(id).model.document;+ } catch (error) {+ Logger.warn("Failed to get Bokeh document", error);+ return null;+ }+}++function isSyncEvent(x: unknown): x is { sync: boolean } {+ const isObject = !!x && typeof x === "object";+ return isObject && "sync" in x;+}++function isDocumentEvent(x: unknown): x is { document: BokehDocument } {+ const isObject = !!x && typeof x === "object";+ return isObject && "document" in x;+}+type Props = IPluginProps<T, PanelData, PluginFunctions>;diff --git a/frontend/src/plugins/impl/panel/utils.ts b/frontend/src/plugins/impl/panel/utils.tsindex d21f25da67..9930df9915 100644--- a/frontend/src/plugins/impl/panel/utils.ts+++ b/frontend/src/plugins/impl/panel/utils.ts@@ -99,8 +99,8 @@ export class EventBuffer<T> {}getAndClear() {- const events = [...this.buffer];- this.clear();+ const events = this.buffer;+ this.buffer = [];return events;}}diff --git a/marimo/_plugins/ui/_impl/from_panel.py b/marimo/_plugins/ui/_impl/from_panel.pyindex 631156fecd..09495c495b 100644--- a/marimo/_plugins/ui/_impl/from_panel.py+++ b/marimo/_plugins/ui/_impl/from_panel.py@@ -15,17 +15,13 @@ from typing import (from marimo import _loggersfrom marimo._output.rich_help import mddocfrom marimo._plugins.ui._core.ui_element import InitializationArgs, UIElement-from marimo._plugins.ui._impl.comm import MarimoComm, MarimoCommManagerfrom marimo._runtime.functions import Function-from marimo._types.ids import WidgetModelIdif TYPE_CHECKING:from panel.viewable import ViewableLOGGER = _loggers.marimo_logger()-COMM_MANAGER = MarimoCommManager()-comm_class: Optional[type[Any]] = Noneloaded_extension: int = 0loaded_extensions: list[str] = []@@ -47,16 +43,25 @@ def _get_comm_class() -> type[Any]:from pyviz_comms import Comm # type: ignore+ from marimo._messaging.notification import (+ UIElementMessageNotification,+ )+ from marimo._messaging.notification_utils import (+ broadcast_notification,+ )+ from marimo._plugins.ui._impl.comm import _ensure_bytes+ from marimo._types.ids import UIElementId+class MarimoPanelComm(Comm): # type: ignoredef __init__(self, *args: Any, **kwargs: Any):super().__init__(*args, **kwargs)- self._comm = MarimoComm(- comm_id=WidgetModelId(str(self.id)),- target_name="panel.comms",- data={},- comm_manager=COMM_MANAGER,- )- self._comm.on_msg(self._handle_msg)+ self._ui_element_id: Optional[str] = None+ # Panel's push() checks `comm._comm` before sending+ # document updates to the frontend. Without a truthy+ # value, backend changes (e.g. DynamicMap overlays) are+ # silently dropped. Setting a sentinel makes push() call+ # send(comm, msg) which routes through our send() override.+ self._comm = True # type: ignore[assignment]if self._on_open:self._on_open({})@@ -69,11 +74,21 @@ def _get_comm_class() -> type[Any]:return dict(msg.message, _buffers=buffers)def send(- self, data: Any = None, metadata: Any = None, buffers: Any = None+ self,+ data: Any = None,+ metadata: Any = None,+ buffers: Any = None, # noqa: ARG002) -> None:- buffers = buffers or []- self.comm.send(- {"content": data}, metadata=metadata, buffers=buffers+ del metadata+ if self._ui_element_id is None:+ return+ raw_buffers = buffers or []+ broadcast_notification(+ UIElementMessageNotification(+ ui_element=UIElementId(self._ui_element_id),+ message={"content": data},+ buffers=[_ensure_bytes(b) for b in raw_buffers],+ ))comm_class = MarimoPanelComm@@ -313,8 +328,13 @@ class panel(UIElement[T, T]):def _handle_msg(self, msg: SendToWidgetArgs) -> None:ref = self._refcomm = self.obj._comms[ref][0] # type: ignore[attr-defined]- msg = comm.decode(msg)- self.obj._on_msg(ref, self._manager, msg) # type: ignore[attr-defined]+ decoded = comm.decode(msg)+ try:+ self.obj._on_msg(ref, self._manager, decoded) # type: ignore[attr-defined]+ except Exception as e:+ # Deserialization or buffer errors from echo/stale patches+ LOGGER.debug("Panel _on_msg error (likely harmless echo): %s", e)+ returncomm.send(data={"type": "ACK"})def _initialize(@@ -323,8 +343,7 @@ class panel(UIElement[T, T]):) -> None:super()._initialize(initialization_args)for comm, _ in self.obj._comms.values(): # type: ignore[attr-defined]- if isinstance(comm.comm, MarimoComm):- comm.comm.ui_element_id = self._id+ comm._ui_element_id = self._iddef _convert_value(self, value: T) -> T:return valuediff --git a/marimo/_smoke_tests/issues/8527-holoviews-dynamicmap.py b/marimo/_smoke_tests/issues/8527-holoviews-dynamicmap.pynew file mode 100644index 0000000000..0181eab0e8--- /dev/null+++ b/marimo/_smoke_tests/issues/8527-holoviews-dynamicmap.py@@ -0,0 +1,57 @@+# /// script+# dependencies = [+# "holoviews==1.22.1",+# "marimo",+# "numpy",+# ]+# requires-python = ">=3.12"+# ///++import marimo++__generated_with = "0.20.2"+app = marimo.App(width="medium")+++def _():+ # Taken directly from https://holoviews.org/reference/streams/bokeh/Selection1D_points.html#selection1d-points++ import numpy as np++ import holoviews as hv+ from holoviews import opts, streams++ hv.extension("bokeh")++ opts.defaults(opts.Points(tools=["box_select", "lasso_select"]))++ # Declare some points+ points = hv.Points(np.random.randn(1000, 2))++ # Declare points as source of selection stream+ selection = streams.Selection1D(source=points)++ # Write function that uses the selection indices to slice points and compute stats+ def selected_info(index):+ selected = points.iloc[index]+ if index:+ label = "Mean x, y: {:.3f}, {:.3f}".format(+ *tuple(selected.array().mean(axis=0))+ )+ else:+ label = "No selection"+ return selected.relabel(label).opts(color="red")++ # Combine points and DynamicMap+ points + hv.DynamicMap(selected_info, streams=[selection])+ return+++def _():+ return+++if __name__ == "__main__":+ app.run()