99
1010from marimo import _loggers
1111from marimo ._data .models import ExternalDataType
12+ from marimo ._output .data .data import sanitize_json_bigint
1213from marimo ._plugins .ui ._impl .tables .format import (
1314 FormatMapping ,
1415 format_value ,
@@ -44,30 +45,28 @@ def __init__(self, data: pd.DataFrame) -> None:
4445 def schema (self ) -> pd .Series [Any ]:
4546 return self ._original_data .dtypes # type: ignore
4647
47- # We override narwhals's to_csv to handle pandas
48+ # We override narwhals's to_csv_str to handle pandas
4849 # headers
49- def to_csv (
50+ def to_csv_str (
5051 self , format_mapping : Optional [FormatMapping ] = None
51- ) -> bytes :
52+ ) -> str :
5253 has_headers = len (self .get_row_headers ()) > 0
5354 # Pandas omits H:M:S for datetimes when H:M:S is identically
5455 # 0; this doesn't play well with our frontend table component,
5556 # so we use an explicit date format.
56- return (
57- self .apply_formatting (format_mapping )
58- ._original_data .to_csv (
59- # By adding %H:%M:%S and %z, we ensure that the
60- # datetime is displayed in the frontend with the
61- # correct timezone.
62- index = has_headers ,
63- date_format = "%Y-%m-%d %H:%M:%S%z" ,
64- )
65- .encode ("utf-8" )
57+ return self .apply_formatting (
58+ format_mapping
59+ )._original_data .to_csv (
60+ # By adding %H:%M:%S and %z, we ensure that the
61+ # datetime is displayed in the frontend with the
62+ # correct timezone.
63+ index = has_headers ,
64+ date_format = "%Y-%m-%d %H:%M:%S%z" ,
6665 )
6766
68- def to_json (
67+ def to_json_str (
6968 self , format_mapping : Optional [FormatMapping ] = None
70- ) -> bytes :
69+ ) -> str :
7170 from pandas .api .types import (
7271 is_complex_dtype ,
7372 is_object_dtype ,
@@ -98,7 +97,9 @@ def to_json(
9897 "Error handling complex or timedelta64 dtype" ,
9998 exc_info = e ,
10099 )
101- return result .to_json (orient = "records" ).encode ("utf-8" )
100+ return sanitize_json_bigint (
101+ result .to_json (orient = "records" )
102+ )
102103
103104 # Flatten row multi-index
104105 if isinstance (result .index , pd .MultiIndex ) or (
@@ -120,10 +121,9 @@ def to_json(
120121 "Indexes with more than one level are not supported properly, call reset_index() to flatten"
121122 )
122123
123- return result .to_json (
124- orient = "records" ,
125- date_format = "iso" ,
126- ).encode ("utf-8" )
124+ return sanitize_json_bigint (
125+ result .to_json (orient = "records" , date_format = "iso" )
126+ )
127127
128128 def to_arrow_ipc (self ) -> bytes :
129129 out = io .BytesIO ()
0 commit comments