Skip to content
Prev Previous commit
Next Next commit
add rich elems test
  • Loading branch information
Light2Dark committed Nov 24, 2025
commit 12dcc412b9298d1e288cc496b3e8e1200c92b076
143 changes: 88 additions & 55 deletions tests/_plugins/ui/_impl/tables/snapshots/narwhals.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,89 @@
[
{
"strings": "a",
"bool": true,
"int": 1,
"float": 1.0,
"datetime": "2021-01-01 00:00:00",
"date": "2021-01-01",
"struct": { "a": 1, "b": 2 },
"list": [1, 2],
"array": [1],
"nulls": null,
"category": "cat",
"set": "{1, 2}",
"imaginary": "(1+2j)",
"time": "12:30:00",
"duration": "1 day, 0:00:00",
"mixed_list": ["1", "two"]
},
{
"strings": "b",
"bool": false,
"int": 2,
"float": 2.0,
"datetime": "2021-01-02 00:00:00",
"date": "2021-01-02",
"struct": { "a": 3, "b": 4 },
"list": [3, 4],
"array": [2],
"nulls": "data",
"category": "dog",
"set": "{3, 4}",
"imaginary": "(3+4j)",
"time": "13:45:00",
"duration": "2 days, 0:00:00",
"mixed_list": ["3.0", "0.0"]
},
{
"strings": "c",
"bool": true,
"int": 3,
"float": 3.0,
"datetime": "2021-01-03 00:00:00",
"date": "2021-01-03",
"struct": { "a": 5, "b": 6 },
"list": [5, 6],
"array": [3],
"nulls": null,
"category": "mouse",
"set": "{5, 6}",
"imaginary": "(5+6j)",
"time": "14:15:00",
"duration": "3 days, 0:00:00",
"mixed_list": [null, "2021-01-01 00:00:00.000000"]
}
]
{
"strings": "a",
"bool": true,
"int": 1,
"float": 1.0,
"datetime": "2021-01-01 00:00:00",
"date": "2021-01-01",
"struct": {
"a": 1,
"b": 2
},
"list": [
1,
2
],
"array": [
1
],
"nulls": null,
"category": "cat",
"set": "{1, 2}",
"imaginary": "(1+2j)",
"time": "12:30:00",
"duration": "1 day, 0:00:00",
"mixed_list": [
"1",
"two"
]
},
{
"strings": "b",
"bool": false,
"int": 2,
"float": 2.0,
"datetime": "2021-01-02 00:00:00",
"date": "2021-01-02",
"struct": {
"a": 3,
"b": 4
},
"list": [
3,
4
],
"array": [
2
],
"nulls": "data",
"category": "dog",
"set": "{3, 4}",
"imaginary": "(3+4j)",
"time": "13:45:00",
"duration": "2 days, 0:00:00",
"mixed_list": [
"3.0",
"0.0"
]
},
{
"strings": "c",
"bool": true,
"int": 3,
"float": 3.0,
"datetime": "2021-01-03 00:00:00",
"date": "2021-01-03",
"struct": {
"a": 5,
"b": 6
},
"list": [
5,
6
],
"array": [
3
],
"nulls": null,
"category": "mouse",
"set": "{5, 6}",
"imaginary": "(5+6j)",
"time": "14:15:00",
"duration": "3 days, 0:00:00",
"mixed_list": [
null,
"2021-01-01 00:00:00.000000"
]
}
]
66 changes: 49 additions & 17 deletions tests/_plugins/ui/_impl/tables/test_narwhals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1718,20 +1718,52 @@ def _get_manager(self) -> NarwhalsTableManager[Any]:


@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
@pytest.mark.parametrize(
"df",
create_dataframes({"button": [button()]}, include=["polars", "pandas"]),
)
def test_rich_elements(df: Any) -> None:
manager = NarwhalsTableManager.from_dataframe(df)
print(manager.to_json_str())
json_data = json.loads(manager.to_json_str())
assert isinstance(json_data, list)
assert isinstance(json_data[0], dict)
assert isinstance(json_data[0]["button"], dict)
assert isinstance(json_data[0]["button"]["_serialized_mime_bundle"], dict)

serialized_mime_bundle = json_data[0]["button"]["_serialized_mime_bundle"]
assert serialized_mime_bundle["mimetype"] == "text/html"
assert serialized_mime_bundle["data"].startswith("<marimo-ui-element")
assert serialized_mime_bundle["data"].endswith("</marimo-ui-element>")
class TestRichElements:
"""Tests for rich elements."""

@pytest.fixture
def rich_data(self) -> dict[str, Any]:
return {"button": [button()]}

@pytest.mark.skipif(
not DependencyManager.pandas.has(), reason="Pandas not installed"
)
def test_pandas(self, rich_data: dict[str, Any]) -> None:
import pandas as pd

df = pd.DataFrame(rich_data)
manager = NarwhalsTableManager.from_dataframe(df)
json_data = json.loads(manager.to_json_str())

# Pandas uses mimetype and data instead of _serialized_mime_bundle
assert isinstance(json_data, list)
assert isinstance(json_data[0], dict)
assert isinstance(json_data[0]["button"], dict)

assert json_data[0]["button"]["mimetype"] == "text/html"
assert json_data[0]["button"]["data"].startswith("<marimo-ui-element")
assert json_data[0]["button"]["data"].endswith("</marimo-ui-element>")

@pytest.mark.parametrize(
"df",
create_dataframes(
{"button": [button()]}, include=["duckdb", "polars", "lazy-polars"]
),
)
def test_rich_elements_default(self, df: Any) -> None:
manager = NarwhalsTableManager.from_dataframe(df)
json_data = json.loads(manager.to_json_str())

assert isinstance(json_data, list)
assert isinstance(json_data[0], dict)
assert isinstance(json_data[0]["button"], dict)
assert isinstance(
json_data[0]["button"]["_serialized_mime_bundle"], dict
)

serialized_mime_bundle = json_data[0]["button"][
"_serialized_mime_bundle"
]
assert serialized_mime_bundle["mimetype"] == "text/html"
assert serialized_mime_bundle["data"].startswith("<marimo-ui-element")
assert serialized_mime_bundle["data"].endswith("</marimo-ui-element>")
Loading