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.

Ensure binary messages are serialized to base64 in Panel Comm layer (#2835)

Philipp Rudiger721cb38c60ee
diff --git a/frontend/src/plugins/impl/panel/PanelPlugin.tsx b/frontend/src/plugins/impl/panel/PanelPlugin.tsx
index b1aececc05..d143508bd9 100644
--- a/frontend/src/plugins/impl/panel/PanelPlugin.tsx
+++ b/frontend/src/plugins/impl/panel/PanelPlugin.tsx
@@ -95,7 +95,7 @@ export const PanelPlugin = createPlugin<T>("marimo-panel")
.input(
z.object({
message: z.unknown(),
- buffers: z.array(z.instanceof(ArrayBuffer)),
+ buffers: z.array(z.string()),
}),
)
.output(z.null().optional()),
@@ -127,8 +127,8 @@ const PanelSlot = (props: Props) => {
const message = {
...window.Bokeh.protocol.Message.create("PATCH-DOC", {}, patch),
};
- const buffers: ArrayBuffer[] = [];
- extractBuffers(message.content, buffers);
+ const buffers: string[] = [];
+ message.content = extractBuffers(message.content, buffers);
functions.send_to_widget({ message, buffers });
});
@@ -173,28 +173,23 @@ const PanelSlot = (props: Props) => {
const receiver = receiverRef.current;
const doc = docRef.current;
- if (!message.content) {
- return;
- }
-
if (!receiver || !doc) {
return;
}
- // Handle ACK messages
- if (typeof message.content !== "string") {
+ const content = message.content;
+ if (content !== null && typeof message.content !== "string") {
+ // Handle ACK messages
if (eventBufferRef.current && eventBufferRef.current.size() > 0) {
processEvents();
}
return;
}
- // Handle non-ACK messages
- const content = message.content;
- if (content.length > 0) {
- receiver.consume(content);
- } else if (buffers !== undefined && buffers.length > 0) {
+ if (buffers?.length > 0) {
receiver.consume(buffers[0].buffer);
+ } else if (content.length) {
+ receiver.consume(content);
} else {
return;
}
diff --git a/frontend/src/plugins/impl/panel/utils.ts b/frontend/src/plugins/impl/panel/utils.ts
index fb440ed5ab..9dc6951c33 100644
--- a/frontend/src/plugins/impl/panel/utils.ts
+++ b/frontend/src/plugins/impl/panel/utils.ts
@@ -30,10 +30,9 @@ export function extractBuffers(
}
return result;
}
- if (value instanceof ArrayBuffer) {
- // TODO: this does not seem correct
+ if (value.to_base64 !== undefined) {
const id = buffers.length;
- buffers.push(value);
+ buffers.push(value.to_base64());
return { id };
}
if (typeof value === "object" && value !== null) {
diff --git a/marimo/_plugins/ui/_impl/from_panel.py b/marimo/_plugins/ui/_impl/from_panel.py
index a0f6e3655f..660fd1cc9e 100644
--- a/marimo/_plugins/ui/_impl/from_panel.py
+++ b/marimo/_plugins/ui/_impl/from_panel.py
@@ -1,6 +1,7 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+import base64
import sys
from dataclasses import dataclass
from typing import (
@@ -65,7 +66,8 @@ def _get_comm_class() -> Type[Any]:
@classmethod
def decode(cls, msg: SendToWidgetArgs) -> dict[str, Any]:
buffers: Dict[int, Any] = {
- i: v for i, v in enumerate(msg.buffers or [])
+ i: memoryview(base64.b64decode(v))
+ for i, v in enumerate(msg.buffers or [])
}
return dict(msg.message, _buffers=buffers)