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: fallback for chart.to_dict() and don't mutate charts (#6972)

Myles Scolnicke6433bc22d7c
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index 78d4e81094..ab9a5e9ddf 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -327,7 +327,10 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
register_transformers()
- self._chart = chart
+ # Make a copy
+ original_chart = chart
+ chart = chart.copy()
+ self._chart = original_chart
if not isinstance(chart, (alt.TopLevelMixin)):
raise ValueError(
@@ -347,7 +350,14 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
if chart.autosize is alt.Undefined:
chart.autosize = "fit-x"
- vega_spec = _parse_spec(chart)
+ try:
+ vega_spec = _parse_spec(chart)
+ except Exception:
+ # Sometimes the changes to width and autosize (above) can cause `.to_dict()` to throw an error
+ # similarly to the issue described in https://github.com/marimo-team/marimo/issues/6244
+ # so we fallback to the original chart.
+ LOGGER.info("Failed to parse spec, using original chart")
+ vega_spec = _parse_spec(original_chart)
if label:
vega_spec["title"] = label
diff --git a/marimo/_smoke_tests/altair_examples/upset.py b/marimo/_smoke_tests/altair_examples/upset.py
new file mode 100644
index 0000000000..309832040f
--- /dev/null
+++ b/marimo/_smoke_tests/altair_examples/upset.py
@@ -0,0 +1,50 @@
+# /// script
+# requires-python = ">=3.13"
+# dependencies = [
+# "altair-upset==0.4.0",
+# "pandas==2.3.2",
+# "pyarrow==22.0.0",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.17.2"
+app = marimo.App(width="medium", sql_output="polars")
+
+
+def _():
+ import marimo as mo
+ import pandas as pd
+ import pyarrow
+ from altair_upset import UpSetAltair
+ return UpSetAltair, mo, pd
+
+
+def _(pd):
+ df = pd.DataFrame({"a": [1, 1, 1, 1, 1, 0], "b": [1, 1, 1, 0, 0, 0]})
+ return (df,)
+
+
+def _(UpSetAltair, df):
+ chart = UpSetAltair(df, sorted(df.columns)).chart
+ return (chart,)
+
+
+def _(chart):
+ chart
+ return
+
+
+def _(chart, mo):
+ mo.ui.altair_chart(chart.copy())
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.py
index c4842bd12f..58ad61738a 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -626,7 +626,24 @@ def test_does_not_modify_original() -> None:
combined1 = alt1 | alt2
combined2 = altair_chart(alt1) | altair_chart(alt2)
- assert combined1 == combined2._chart
+ assert combined1.to_dict() == combined2._chart.to_dict()
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_creating_altair_chart_does_not_mutate_original() -> None:
+ import altair as alt
+
+ data = {"values": [1, 2, 3]}
+ original_chart = alt.Chart(data).mark_point().encode(x="values:Q")
+
+ # Store the original spec
+ original_spec = original_chart.to_dict()
+
+ # Create marimo altair_chart wrapper
+ _ = altair_chart(original_chart)
+
+ # Verify the original chart hasn't been mutated
+ assert original_chart.to_dict() == original_spec
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")