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 cross-view interactions (e.g., brush selections) breaking in concatenated Altair charts when using `mo.ui.altair_chart`. (#7673)

Myles Scolnick63519171d266
diff --git a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts
index bdcee33caa..2755cac08e 100644
--- a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts
@@ -1194,4 +1194,78 @@ describe("makeSelectable", () => {
expect(newSpec).toMatchSnapshot();
expect(parse(newSpec)).toBeDefined();
});
+
+ it("should preserve vconcat with existing selection params unchanged (issue #7668)", () => {
+ // This test reproduces the issue from #7668:
+ // Altair charts with cross-view interactions (e.g., brush selection)
+ // should not be modified by makeSelectable to preserve the interaction.
+ const spec = {
+ vconcat: [
+ {
+ mark: "area",
+ encoding: {
+ x: {
+ field: "x",
+ type: "quantitative",
+ scale: { domain: { param: "brush", encoding: "x" } },
+ },
+ y: { field: "y", type: "quantitative" },
+ },
+ },
+ {
+ mark: "area",
+ encoding: {
+ x: { field: "x", type: "quantitative" },
+ y: { field: "y", type: "quantitative" },
+ },
+ },
+ ],
+ params: [
+ {
+ name: "brush",
+ select: { type: "interval", encodings: ["x"] },
+ views: ["view_1"],
+ },
+ ],
+ } as VegaLiteSpec;
+
+ const newSpec = makeSelectable(spec, {});
+
+ // The spec should be returned unchanged to preserve cross-view interactions
+ expect(newSpec).toEqual(spec);
+ expect(getSelectionParamNames(newSpec)).toEqual(["brush"]);
+ });
+
+ it("should preserve hconcat with existing selection params unchanged (issue #7668)", () => {
+ const spec = {
+ hconcat: [
+ {
+ mark: "point",
+ encoding: {
+ x: { field: "x", type: "quantitative" },
+ y: { field: "y", type: "quantitative" },
+ },
+ },
+ {
+ mark: "point",
+ encoding: {
+ x: { field: "x", type: "quantitative" },
+ y: { field: "y", type: "quantitative" },
+ },
+ },
+ ],
+ params: [
+ {
+ name: "my_selection",
+ select: { type: "point", encodings: ["x", "y"] },
+ },
+ ],
+ } as VegaLiteSpec;
+
+ const newSpec = makeSelectable(spec, {});
+
+ // The spec should be returned unchanged
+ expect(newSpec).toEqual(spec);
+ expect(getSelectionParamNames(newSpec)).toEqual(["my_selection"]);
+ });
});
diff --git a/frontend/src/plugins/impl/vega/make-selectable.ts b/frontend/src/plugins/impl/vega/make-selectable.ts
index 1fb89f5a45..7695ed8e8f 100644
--- a/frontend/src/plugins/impl/vega/make-selectable.ts
+++ b/frontend/src/plugins/impl/vega/make-selectable.ts
@@ -303,6 +303,14 @@ export function makeSelectable<T extends VegaLiteSpec>(
chartSelection = false;
}
+ // For vconcat/hconcat with existing chart params, return unchanged.
+ // This preserves cross-view interactions (e.g., brush selections in
+ // concatenated charts). See issue #7668.
+ const isCompoundSpec = "vconcat" in spec || "hconcat" in spec;
+ if (hasChartParam && isCompoundSpec) {
+ return spec;
+ }
+
if ("vconcat" in spec) {
const subSpecs = spec.vconcat.map((subSpec) =>
"mark" in subSpec
diff --git a/marimo/_smoke_tests/issues/7668_altair_concat_interactions.py b/marimo/_smoke_tests/issues/7668_altair_concat_interactions.py
new file mode 100644
index 0000000000..bd4cbd0ed3
--- /dev/null
+++ b/marimo/_smoke_tests/issues/7668_altair_concat_interactions.py
@@ -0,0 +1,53 @@
+import marimo
+
+__generated_with = "0.18.4"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ import altair as alt
+ import numpy as np
+ import pyarrow
+ import pandas as pd
+ return alt, mo, np, pd
+
+
+def _(alt, np, pd):
+ source = pd.DataFrame({"x": np.random.rand(100), "y": np.random.rand(100)})
+
+ brush = alt.selection_interval(encodings=["x"], value={"x": [0, 0.5]})
+
+ base = (
+ alt.Chart(source, width=600, height=200)
+ .mark_area()
+ .encode(x="x:Q", y="y:Q")
+ )
+
+ upper = base.encode(alt.X("x:Q").scale(domain=brush))
+
+ lower = base.properties(height=60).add_params(brush)
+
+ # Brush interaction across views works:
+ upper & lower
+ return lower, upper
+
+
+def _(lower, mo, upper):
+ # Brush interaction (no longer) breaks:
+ mo.ui.altair_chart(upper & lower)
+ return
+
+
+def _(alt, lower, mo, upper):
+ # Brush interaction works:
+ mo.ui.altair_chart(alt.vconcat(upper & lower))
+ 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 0820b2cbd7..7e5ba9c3b9 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -1480,6 +1480,28 @@ def test_has_selection_param() -> None:
layered = alt.layer(chart, rule)
assert _has_selection_param(layered) is True
+ # VConcatChart with selection in nested chart (issue #7668)
+ chart_with_selection = (
+ alt.Chart()
+ .mark_point()
+ .add_params(alt.selection_interval(name="brush", encodings=["x"]))
+ )
+ chart_without_selection = alt.Chart().mark_point()
+ vconcat = chart_with_selection & chart_without_selection
+ assert _has_selection_param(vconcat) is True
+
+ # HConcatChart with selection in nested chart
+ hconcat = chart_with_selection | chart_without_selection
+ assert _has_selection_param(hconcat) is True
+
+ # Nested VConcatChart
+ nested_vconcat = alt.vconcat(vconcat, chart_without_selection)
+ assert _has_selection_param(nested_vconcat) is True
+
+ # VConcatChart without any selection
+ vconcat_no_selection = chart_without_selection & chart_without_selection
+ assert _has_selection_param(vconcat_no_selection) is False
+
# Invalid chart
chart = None
assert _has_selection_param(chart) is False
@@ -1513,6 +1535,24 @@ def test_has_legend_param() -> None:
layered = alt.layer(chart, rule)
assert _has_legend_param(layered) is False
+ # VConcatChart with legend param in nested chart
+ chart_with_legend = (
+ alt.Chart()
+ .mark_point()
+ .add_params(alt.selection_point(fields=["color"], bind="legend"))
+ )
+ chart_without_legend = alt.Chart().mark_point()
+ vconcat = chart_with_legend & chart_without_legend
+ assert _has_legend_param(vconcat) is True
+
+ # HConcatChart with legend param in nested chart
+ hconcat = chart_with_legend | chart_without_legend
+ assert _has_legend_param(hconcat) is True
+
+ # VConcatChart without any legend param
+ vconcat_no_legend = chart_without_legend & chart_without_legend
+ assert _has_legend_param(vconcat_no_legend) is False
+
# Invalid chart
chart = None
assert _has_legend_param(chart) is False