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.

improv: privatize mime bundle (#4225)

Shahmir Varqha11974a5ac330
diff --git a/frontend/src/components/data-table/__tests__/columns.test.tsx b/frontend/src/components/data-table/__tests__/columns.test.tsx
index 5de9e2ac77..e53ff2e42b 100644
--- a/frontend/src/components/data-table/__tests__/columns.test.tsx
+++ b/frontend/src/components/data-table/__tests__/columns.test.tsx
@@ -287,6 +287,12 @@ describe("getMimeValues", () => {
expect(getMimeValues(value)).toEqual([mimeValue]);
});
+ it("should return array with MimeValue when input has _serialized_mime_bundle", () => {
+ const mimeValue = { mimetype: "text/plain", data: "test data" };
+ const value = { _serialized_mime_bundle: mimeValue };
+ expect(getMimeValues(value)).toEqual([mimeValue]);
+ });
+
it("should return array of MimeValues when input is an array of MimeValues", () => {
const values = [
{ mimetype: "text/plain", data: "test data 1" },
@@ -316,6 +322,12 @@ describe("getMimeValues", () => {
).toBeUndefined();
});
+ it("should return undefined for invalid _serialized_mime_bundle", () => {
+ expect(
+ getMimeValues({ _serialized_mime_bundle: "not a mime value" }),
+ ).toBeUndefined();
+ });
+
it("should return undefined for array with non-MimeValue items", () => {
const values = [
{ mimetype: "text/plain", data: "test data" },
diff --git a/frontend/src/components/data-table/mime-cell.tsx b/frontend/src/components/data-table/mime-cell.tsx
index b08a0c5936..d4db695f6c 100644
--- a/frontend/src/components/data-table/mime-cell.tsx
+++ b/frontend/src/components/data-table/mime-cell.tsx
@@ -44,10 +44,12 @@ export function getMimeValues(value: unknown): MimeValue[] | undefined {
const hasSerializedMimeBundle =
typeof value === "object" &&
value !== null &&
- "serialized_mime_bundle" in value;
+ ("_serialized_mime_bundle" in value || "serialized_mime_bundle" in value);
if (hasSerializedMimeBundle) {
- const serializedMimeBundle = value.serialized_mime_bundle;
+ const obj = value as Record<string, unknown>;
+ const serializedMimeBundle =
+ obj._serialized_mime_bundle || obj.serialized_mime_bundle;
if (isMimeValue(serializedMimeBundle)) {
return [serializedMimeBundle];
}
diff --git a/marimo/_output/hypertext.py b/marimo/_output/hypertext.py
index 789f6b89db..abf162751a 100644
--- a/marimo/_output/hypertext.py
+++ b/marimo/_output/hypertext.py
@@ -73,8 +73,10 @@ class Html(MIME):
# Some libraries (e.g. polars) will serialize dataclasses so we add this
# field to serialize the mimetype. This is to support rich display in tables/dfs.
- serialized_mime_bundle: dict[Literal["mimetype", "data"], str] = field(
- default_factory=dict
+ _serialized_mime_bundle: dict[Literal["mimetype", "data"], str] = field(
+ default_factory=dict,
+ repr=False,
+ init=False,
)
def __init__(self, text: str) -> None:
@@ -84,10 +86,17 @@ class Html(MIME):
"""
self._text = text
mimetype, data = self._mime_()
- self.serialized_mime_bundle = {
+
+ self._serialized_mime_bundle = {
"mimetype": mimetype,
"data": data,
}
+ # Whenever _serialized_mime_bundle is set, ensure a public copy exists.
+ # This avoids declaring a public attribute (does not show up in docs)
+ # Pandas does not serialize private variables, so we need this.
+ self.__setattr__(
+ "serialized_mime_bundle", self._serialized_mime_bundle
+ )
# A list of the virtual file names referenced by this HTML element.
self._virtual_filenames: list[str] = []
diff --git a/marimo/_smoke_tests/tables/rich_elements.py b/marimo/_smoke_tests/tables/rich_elements.py
index ac9f8a1b72..cf0ffa6982 100644
--- a/marimo/_smoke_tests/tables/rich_elements.py
+++ b/marimo/_smoke_tests/tables/rich_elements.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.11.24"
+__generated_with = "0.11.26"
app = marimo.App(width="medium")
diff --git a/tests/_output/test_hypertext.py b/tests/_output/test_hypertext.py
index 3e339d3a93..77510286ea 100644
--- a/tests/_output/test_hypertext.py
+++ b/tests/_output/test_hypertext.py
@@ -1,5 +1,8 @@
from __future__ import annotations
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
from marimo._output.hypertext import (
Html,
_js,
@@ -7,6 +10,7 @@ from marimo._output.hypertext import (
)
from marimo._plugins.ui._impl.batch import batch as batch_plugin
from marimo._plugins.ui._impl.input import button
+from marimo._plugins.ui._impl.table import table
def test_html_initialization():
@@ -20,7 +24,7 @@ def test_html_mime():
assert mime_type == "text/html"
assert content == "<p>Test</p>"
- assert html.serialized_mime_bundle == {
+ assert html._serialized_mime_bundle == {
"mimetype": "text/html",
"data": "<p>Test</p>",
}
@@ -210,3 +214,38 @@ def test_html_patch_for_non_interactive_output():
"text/html",
"<web-component>Hello</web-component>",
)
+
+
+ not DependencyManager.polars.has() or not DependencyManager.pandas.has(),
+ reason="Pandas and Polars not installed",
+)
+def test_html_rich_elems():
+ tbl = table({"button": button()})
+ html = Html(tbl)
+
+ assert isinstance(html._serialized_mime_bundle, dict)
+ assert html._serialized_mime_bundle == {
+ "mimetype": "text/html",
+ "data": tbl,
+ }
+
+ import pandas as pd
+
+ df = pd.DataFrame({"button": [button()]})
+ html = Html(df)
+ # ensure public copy exists
+ assert hasattr(html, "serialized_mime_bundle") is True
+ assert html.serialized_mime_bundle == {
+ "mimetype": "text/html",
+ "data": df,
+ }
+
+ import polars as pl
+
+ df = pl.DataFrame({"button": button()})
+ html = Html(df)
+ assert html._serialized_mime_bundle == {
+ "mimetype": "text/html",
+ "data": df,
+ }