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.

fix: checkbox width fix: altair timestamp on pandas (#4933)

Myles Scolnick3ce961b925fe
diff --git a/marimo/_plugins/ui/_impl/charts/altair_transformer.py b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
index 8c573bf20e..23b3cb313b 100644
--- a/marimo/_plugins/ui/_impl/charts/altair_transformer.py
+++ b/marimo/_plugins/ui/_impl/charts/altair_transformer.py
@@ -9,6 +9,7 @@ from narwhals.typing import IntoDataFrame
import marimo._output.data.data as mo_data
from marimo import _loggers
+from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins.ui._impl.tables.utils import (
get_table_manager,
get_table_manager_or_none,
@@ -112,14 +113,25 @@ def _data_to_json_string(data: _DataType) -> str:
def _data_to_csv_string(data: _DataType) -> str:
"""Return a CSV string representation of the input data"""
data = _maybe_sanitize_dataframe(data)
-
- return get_table_manager(data).to_csv().decode("utf-8")
+ return get_table_manager(data).to_csv_str()
def _maybe_sanitize_dataframe(data: Any) -> Any:
"""Sanitize a pandas or narwhals DataFrame for JSON serialization"""
import altair as alt
+ # First try to sanitize with sanitize_pandas_dataframe
+ # because sanitize_narwhals_dataframe on pandas does not
+ # produce a correct result.
+ if DependencyManager.pandas.imported():
+ import pandas as pd
+
+ if isinstance(
+ data, pd.DataFrame
+ ) and "sanitize_pandas_dataframe" in dir(alt.utils):
+ return alt.utils.sanitize_pandas_dataframe(data) # type: ignore[attr-defined]
+
+ # Then try to sanitize with sanitize_narwhals_dataframe
if can_narwhalify(data) and "sanitize_narwhals_dataframe" in dir(
alt.utils
):
diff --git a/marimo/_smoke_tests/issues/3564_altair_timezone.py b/marimo/_smoke_tests/issues/3564_altair_timezone.py
index 26f1dc9ebe..59b64eb38e 100644
--- a/marimo/_smoke_tests/issues/3564_altair_timezone.py
+++ b/marimo/_smoke_tests/issues/3564_altair_timezone.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.10.16"
+__generated_with = "0.13.9"
app = marimo.App(width="medium")
@@ -71,9 +71,45 @@ def _(alt, df_lib_select, mo, timedelta, to_display):
.properties(width=800, height=200)
)
chart = mo.ui.altair_chart(bars)
+ return bars, chart
- mo.vstack([bars, chart])
- return bars, chart, df_buckets, df_lib
+
+def _(bars):
+ bars
+ return
+
+
+def _(chart):
+ chart
+ return
+
+
+def _(alt, bars, mo):
+ with alt.data_transformers.enable("marimo_json"):
+ mo.output.replace(bars)
+ return
+
+
+def _(alt, bars, mo):
+ with alt.data_transformers.enable("marimo_csv"):
+ mo.output.replace(bars)
+ return
+
+
+def _(bars, mo):
+ mo.ui.altair_chart(bars)
+ return
+
+
+def _(bars, mo):
+ mo.iframe(bars.to_html())
+ return
if __name__ == "__main__":