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.

use enc hook function and add more handling (#7290)

Shahmir Varqha55cdfa048168
diff --git a/marimo/_messaging/msgspec_encoder.py b/marimo/_messaging/msgspec_encoder.py
index 35e4bc1191..53ca8a4708 100644
--- a/marimo/_messaging/msgspec_encoder.py
+++ b/marimo/_messaging/msgspec_encoder.py
@@ -6,6 +6,7 @@ from __future__ import annotations
import collections
import datetime
import fractions
+import uuid
from math import isnan
from pathlib import PurePath
from typing import Any
@@ -29,13 +30,7 @@ def enc_hook(obj: Any) -> Any:
if isinstance(obj, range):
return list(obj)
- if isinstance(obj, complex):
- return str(obj)
-
- if isinstance(obj, fractions.Fraction):
- return str(obj)
-
- if isinstance(obj, PurePath):
+ if isinstance(obj, (complex, fractions.Fraction, PurePath, uuid.UUID)):
return str(obj)
if DependencyManager.numpy.imported():
@@ -87,7 +82,17 @@ def enc_hook(obj: Any) -> Any:
),
):
return str(obj)
- if isinstance(obj, (pd.TimedeltaIndex, pd.DatetimeIndex)):
+ if obj is pd.NaT:
+ return str(obj)
+ if isinstance(
+ obj,
+ (
+ pd.TimedeltaIndex,
+ pd.DatetimeIndex,
+ pd.IntervalIndex,
+ pd.PeriodIndex,
+ ),
+ ):
return obj.astype(str).tolist()
if isinstance(obj, pd.MultiIndex):
return obj.to_list()
@@ -190,7 +195,10 @@ def enc_hook(obj: Any) -> Any:
return obj
# Handle datetime types
- if isinstance(obj, (datetime.datetime, datetime.timedelta, datetime.date)):
+ if isinstance(
+ obj,
+ (datetime.datetime, datetime.timedelta, datetime.date, datetime.time),
+ ):
return str(obj)
# Handle None
diff --git a/marimo/_output/data/data.py b/marimo/_output/data/data.py
index e5130cc801..0bfbd634d7 100644
--- a/marimo/_output/data/data.py
+++ b/marimo/_output/data/data.py
@@ -5,7 +5,7 @@ import base64
import io
from typing import Any, Union
-from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.msgspec_encoder import enc_hook
from marimo._plugins.core.media import is_data_empty
from marimo._runtime.virtual_file import (
EMPTY_VIRTUAL_FILE,
@@ -238,27 +238,5 @@ def sanitize_json_bigint(
convert_bigint(as_json),
indent=None,
separators=(",", ":"),
- default=custom_json,
+ default=enc_hook,
)
-
-
-def custom_json(obj: Any) -> Any:
- if hasattr(obj, "_mime_"):
- mimetype, data = obj._mime_()
- return {"mimetype": mimetype, "data": data}
-
- if DependencyManager.pandas.imported():
- import pandas as pd
-
- # TODO: Port this logic over to msgspec encoder
- if isinstance(
- obj,
- (
- pd.TimedeltaIndex,
- pd.DatetimeIndex,
- pd.IntervalIndex,
- pd.PeriodIndex,
- ),
- ):
- return obj.astype(str).tolist()
- return str(obj)
diff --git a/marimo/_smoke_tests/tables/complex_types.py b/marimo/_smoke_tests/tables/complex_types.py
index a86284ee28..3c0b157ac6 100644
--- a/marimo/_smoke_tests/tables/complex_types.py
+++ b/marimo/_smoke_tests/tables/complex_types.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.17.6"
+__generated_with = "0.17.8"
app = marimo.App(width="medium")
@@ -148,6 +148,26 @@ def _(df, mo):
return
+def _(mo, pd, pl):
+ import uuid
+
+ uuid_data = {
+ "id": [
+ uuid.UUID("00000000-0000-0000-0000-000000000000"),
+ uuid.UUID("ffffffff-ffff-ffff-ffff-ffffffffffff"),
+ uuid.UUID("123e4567-e89b-12d3-a456-426614174000"),
+ ],
+ "name": ["test1", "test2", "test3"],
+ }
+
+ uuid_df = pd.DataFrame(uuid_data)
+ uuid_polars = pl.DataFrame(uuid_data)
+
+ mo.vstack([mo.plain(uuid_df), uuid_df, uuid_polars])
+ return
+
+
@app.cell
def _(mo):
import pandas as pd
diff --git a/tests/_plugins/ui/_impl/tables/snapshots/narwhals.json b/tests/_plugins/ui/_impl/tables/snapshots/narwhals.json
index fc5129e88c..31ca4a95a8 100644
--- a/tests/_plugins/ui/_impl/tables/snapshots/narwhals.json
+++ b/tests/_plugins/ui/_impl/tables/snapshots/narwhals.json
@@ -19,7 +19,10 @@
],
"nulls": null,
"category": "cat",
- "set": "{1, 2}",
+ "set": [
+ 1,
+ 2
+ ],
"imaginary": "(1+2j)",
"time": "12:30:00",
"duration": "1 day, 0:00:00",
@@ -48,7 +51,10 @@
],
"nulls": "data",
"category": "dog",
- "set": "{3, 4}",
+ "set": [
+ 3,
+ 4
+ ],
"imaginary": "(3+4j)",
"time": "13:45:00",
"duration": "2 days, 0:00:00",
@@ -77,7 +83,10 @@
],
"nulls": null,
"category": "mouse",
- "set": "{5, 6}",
+ "set": [
+ 5,
+ 6
+ ],
"imaginary": "(5+6j)",
"time": "14:15:00",
"duration": "3 days, 0:00:00",
diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv
index c9264b19e4..2e800ed469 100644
--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv
+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv
@@ -1,4 +1,4 @@
-strings,bool,int,float,datetime,date,nanosecond,datetime_index,period_range,interval_range,delta,none,infs,struct,list,nulls,category,set,imaginary,time,duration,mixed_list,bytes
-a,True,1,1.0,2021-01-01,2021-01-01,2017-03-22 15:16:45,"DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)",2017Q1,"(2017-01-01 00:00:00, 2017-01-02 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 1, 'b': 2}","[1, 2]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'
-b,False,2,2.0,2021-01-02,2021-01-02,2017-07-14 02:40:00,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q2,"(2017-01-02 00:00:00, 2017-01-03 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 3, 'b': 4}","[3, 4]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'
-c,True,3,3.0,2021-01-03,2021-01-03,2017-11-06 20:26:40,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q3,"(2017-01-03 00:00:00, 2017-01-04 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT], dtype='timedelta64[ns]', freq=None)",,-inf,"{'a': 5, 'b': 6}","[5, 6]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'
+strings,bool,int,float,datetime,date,nanosecond,datetime_index,period_range,interval_range,delta,none,infs,struct,list,nested_lists,objects,nested_objects,nulls,category,set,imaginary,time,duration,mixed_list,bytes
+a,True,1,1.0,2021-01-01,2021-01-01,2017-03-22 15:16:45,"DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)",2017Q1,"(2017-01-01 00:00:00, 2017-01-02 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 1, 'b': 2}","[1, 2]","[[1, 2]]","{'a': 1, 'b': 2}","[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'
+b,False,2,2.0,2021-01-02,2021-01-02,2017-07-14 02:40:00,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q2,"(2017-01-02 00:00:00, 2017-01-03 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 3, 'b': 4}","[3, 4]","[[3, 4]]","{'a': 3, 'b': 4}","[{'a': 5, 'b': 6}, {'a': 7, 'b': 8}]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'
+c,True,3,3.0,2021-01-03,2021-01-03,2017-11-06 20:26:40,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q3,"(2017-01-03 00:00:00, 2017-01-04 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT], dtype='timedelta64[ns]', freq=None)",,-inf,"{'a': 5, 'b': 6}","[5, 6]","[[5, 6]]","{'a': 5, 'b': 6}","[{'a': 9, 'b': 10}, {'a': 11, 'b': 12}]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'
diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json b/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json
index ab331e0cd2..5c6e16b1a5 100644
--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json
+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json
@@ -104,6 +104,27 @@
"object"
]
],
+ [
+ "nested_lists",
+ [
+ "string",
+ "object"
+ ]
+ ],
+ [
+ "objects",
+ [
+ "string",
+ "object"
+ ]
+ ],
+ [
+ "nested_objects",
+ [
+ "string",
+ "object"
+ ]
+ ],
[
"nulls",
[
diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json b/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
index 72e0eae27d..bfd4a96c76 100644
--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.json
@@ -27,9 +27,32 @@
1,
2
],
+ "nested_lists": [
+ [
+ 1,
+ 2
+ ]
+ ],
+ "objects": {
+ "a": 1,
+ "b": 2
+ },
+ "nested_objects": [
+ {
+ "a": 1,
+ "b": 2
+ },
+ {
+ "a": 3,
+ "b": 4
+ }
+ ],
"nulls": null,
"category": "cat",
- "set": "{1, 2}",
+ "set": [
+ 1,
+ 2
+ ],
"imaginary": "(1+2j)",
"time": "12:30:00",
"duration": "1 days 00:00:00",
@@ -68,9 +91,32 @@
3,
4
],
+ "nested_lists": [
+ [
+ 3,
+ 4
+ ]
+ ],
+ "objects": {
+ "a": 3,
+ "b": 4
+ },
+ "nested_objects": [
+ {
+ "a": 5,
+ "b": 6
+ },
+ {
+ "a": 7,
+ "b": 8
+ }
+ ],
"nulls": "data",
"category": "dog",
- "set": "{3, 4}",
+ "set": [
+ 3,
+ 4
+ ],
"imaginary": "(3+4j)",
"time": "13:45:00",
"duration": "2 days 00:00:00",
@@ -110,9 +156,32 @@
5,
6
],
+ "nested_lists": [
+ [
+ 5,
+ 6
+ ]
+ ],
+ "objects": {
+ "a": 5,
+ "b": 6
+ },
+ "nested_objects": [
+ {
+ "a": 9,
+ "b": 10
+ },
+ {
+ "a": 11,
+ "b": 12
+ }
+ ],
"nulls": null,
"category": "mouse",
- "set": "{5, 6}",
+ "set": [
+ 5,
+ 6
+ ],
"imaginary": "(5+6j)",
"time": "14:15:00",
"duration": "3 days 00:00:00",
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index 33b507a2bc..91d70d50eb 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -122,6 +122,17 @@ class TestPandasTableManager(unittest.TestCase):
{"a": 5, "b": 6},
],
"list": pd.Series([[1, 2], [3, 4], [5, 6]]),
+ "nested_lists": pd.Series([[[1, 2]], [[3, 4]], [[5, 6]]]),
+ "objects": pd.Series(
+ [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]
+ ),
+ "nested_objects": pd.Series(
+ [
+ [{"a": 1, "b": 2}, {"a": 3, "b": 4}],
+ [{"a": 5, "b": 6}, {"a": 7, "b": 8}],
+ [{"a": 9, "b": 10}, {"a": 11, "b": 12}],
+ ]
+ ),
"nulls": pd.Series([None, "data", None]),
"category": pd.Categorical(["cat", "dog", "mouse"]),
"set": [set([1, 2]), set([3, 4]), set([5, 6])],