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: detect inline datasets for altair_chart (#5682)

Shahmir Varqha6280d9ac90e8
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index 25e9b7d890..0b2be2b55e 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -569,7 +569,7 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
@property
def value(self) -> ChartDataType:
- from altair import Undefined
+ from altair import InlineData, InlineDataset, Undefined
value = super().value
if value is Undefined: # type: ignore
@@ -579,6 +579,12 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
"Use `.apply_selection(df)` to filter a DataFrame"
" based on the selection.",
)
+ elif isinstance(value, (InlineData, InlineDataset)):
+ sys.stderr.write(
+ "The underlying chart data is not available with inline specification. "
+ "Use `.apply_selection(df)` to filter a DataFrame"
+ " based on the selection.",
+ )
return value
@value.setter
diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.py
index 49ee40fa22..24a40142cb 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -2,8 +2,10 @@
from __future__ import annotations
import datetime
+import io
import json
import sys
+from contextlib import redirect_stderr
from typing import TYPE_CHECKING, Any
from unittest.mock import Mock
@@ -865,6 +867,33 @@ def test_apply_selection(df: IntoDataFrame):
assert all(filtered_data["category"] == "A")
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_value_is_not_available() -> None:
+ import altair as alt
+
+ # inline charts
+ chart_spec = {
+ "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
+ "data": {"values": [{"x": 1, "y": 1}]},
+ "mark": "point",
+ "encoding": {
+ "x": {"field": "x", "type": "quantitative"},
+ "y": {"field": "y", "type": "quantitative"},
+ },
+ }
+
+ marimo_chart = altair_chart(alt.Chart.from_dict(chart_spec))
+
+ # check if calling marimo_chart.value writes to stderr
+ with io.StringIO() as buf, redirect_stderr(buf):
+ _ = marimo_chart.value
+ stderr_output = buf.getvalue()
+ assert (
+ "Use `.apply_selection(df)` to filter a DataFrame based on the selection."
+ in stderr_output
+ )
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
def test_chart_with_url_data():
import altair as alt