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: box-plots are not selectable (#4063)

Myles Scolnick33517b1cb52a
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 09154de417..e620160aa6 100644
--- a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts
@@ -556,4 +556,16 @@ describe("makeSelectable", () => {
]
`);
});
+
+ it.each(["errorbar", "errorband", "boxplot"])(
+ "should return the same spec if mark is %s",
+ (mark) => {
+ const spec = {
+ mark,
+ } as unknown as VegaLiteSpec;
+ const newSpec = makeSelectable(spec, {});
+ expect(newSpec).toEqual(spec);
+ expect(getSelectionParamNames(newSpec)).toEqual([]);
+ },
+ );
});
diff --git a/frontend/src/plugins/impl/vega/make-selectable.ts b/frontend/src/plugins/impl/vega/make-selectable.ts
index 2bec778241..57eab2d036 100644
--- a/frontend/src/plugins/impl/vega/make-selectable.ts
+++ b/frontend/src/plugins/impl/vega/make-selectable.ts
@@ -70,6 +70,10 @@ export function makeSelectable<T extends VegaLiteSpec>(
if (!("mark" in spec)) {
return spec;
}
+ // error, errorbar, boxplot are not interactive
+ if (!Marks.isInteractive(spec.mark)) {
+ return spec;
+ }
let resolvedSpec: VegaLiteUnitSpec = spec;
resolvedSpec = makeLegendSelectable(resolvedSpec, fieldSelection);
@@ -210,7 +214,12 @@ function makeChartInteractive<T extends GenericVegaSpec>(spec: T): T {
return spec;
}
- const mark = Marks.getMarkType(spec.mark);
+ let mark: Mark;
+ try {
+ mark = Marks.getMarkType(spec.mark);
+ } catch {
+ return spec;
+ }
// We don't do anything if the mark is text
if (mark === "text") {
diff --git a/frontend/src/plugins/impl/vega/marks.ts b/frontend/src/plugins/impl/vega/marks.ts
index 256e9140ff..a275afd20d 100644
--- a/frontend/src/plugins/impl/vega/marks.ts
+++ b/frontend/src/plugins/impl/vega/marks.ts
@@ -1,13 +1,19 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { type AnyMark, Mark } from "./types";
+const NON_INTERACTIVE_MARKS = new Set(["boxplot", "errorband", "errorbar"]);
+
export const Marks = {
getMarkType(mark: AnyMark): Mark {
const type = typeof mark === "string" ? mark : mark.type;
- if (type === "boxplot" || type === "errorband" || type === "errorbar") {
+ if (NON_INTERACTIVE_MARKS.has(type)) {
throw new Error("Not supported");
}
- return type;
+ return type as Mark;
+ },
+ isInteractive(mark: AnyMark): boolean {
+ const type = typeof mark === "string" ? mark : mark.type;
+ return !NON_INTERACTIVE_MARKS.has(type);
},
makeClickable(mark: AnyMark): AnyMark {
const type = typeof mark === "string" ? mark : mark.type;
diff --git a/marimo/_smoke_tests/altair/boxplot.py b/marimo/_smoke_tests/altair/boxplot.py
new file mode 100644
index 0000000000..7fb3cc494e
--- /dev/null
+++ b/marimo/_smoke_tests/altair/boxplot.py
@@ -0,0 +1,83 @@
+import marimo
+
+__generated_with = "0.11.17"
+app = marimo.App(width="medium")
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md(r"""# Box plots""")
+ return
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md(r"""## Example 1""")
+ return
+
+
+def _():
+ import altair as alt
+ import polars as pl
+
+ # Create a sample dataframe
+ data = pl.DataFrame(
+ {
+ "category": ["A", "A", "A", "B", "B", "B", "C", "C", "C"],
+ "value": [10, 20, 15, 25, 30, 5, 50, 55, 45],
+ }
+ )
+
+ # Convert to Pandas DataFrame for compatibility with Altair
+ data_pd = data.to_pandas()
+
+ # Create a boxplot
+ boxplot = alt.Chart(data_pd).mark_boxplot().encode(x="category:O", y="value:Q")
+
+ boxplot
+ return alt, boxplot, data, data_pd, pl
+
+
+def _(boxplot, mo):
+ mo.ui.altair_chart(boxplot)
+ return
+
+
[email protected](hide_code=True)
+def _(mo):
+ mo.md(r"""## Example 2""")
+ return
+
+
+def _(pl):
+ import random
+
+ df = pl.DataFrame(
+ {
+ "val": random.sample(range(1, 100), 50),
+ "grp": random.choices(["A", "B", "C"], k=50),
+ }
+ )
+
+ boxplot2 = df.plot.boxplot(y="grp", x="val", color="grp")
+ boxplot2
+ return boxplot2, df, random
+
+
+def _(boxplot2, mo):
+ mo.ui.altair_chart(boxplot2)
+ return
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
+if __name__ == "__main__":
+ app.run()