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: edge-case for mo.ui.altair_chart point selection (#329)

Myles Scolnick7a7b7186c8b9
diff --git a/frontend/src/plugins/impl/vega/VegaPlugin.tsx b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
index 0b4e20e22f..4837816c7c 100644
--- a/frontend/src/plugins/impl/vega/VegaPlugin.tsx
+++ b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
@@ -20,6 +20,7 @@ import { fixRelativeUrl } from "./fix-relative-url";
import "./vega.css";
import { useThemeForPlugin } from "@/theme/useTheme";
+import { Objects } from "@/utils/objects";
interface Data {
spec: VegaLiteSpec;
@@ -172,8 +173,12 @@ const LoadedVegaComponent = ({
// Debounce each signal listener, otherwise we may create expensive requests
acc[name] = debounce((signalName, signalValue) => {
Logger.debug("[Vega signal]", signalName, signalValue);
+
handleUpdateValue({
- [signalName]: signalValue,
+ [signalName]: Objects.mapValues(
+ signalValue as object,
+ convertSetToList
+ ),
});
}, 100);
return acc;
@@ -218,3 +223,13 @@ const actions = {
source: false,
compiled: false,
};
+
+/**
+ * Convert any sets to a list before passing to the BE
+ */
+function convertSetToList(value: unknown): unknown {
+ if (value instanceof Set) {
+ return [...value];
+ }
+ return value;
+}
diff --git a/frontend/src/utils/objects.ts b/frontend/src/utils/objects.ts
index 995afba865..04f257ff92 100644
--- a/frontend/src/utils/objects.ts
+++ b/frontend/src/utils/objects.ts
@@ -4,6 +4,10 @@ export const Objects = {
obj: Record<K, T>,
mapper: (value: T) => U
): Record<K, U> {
+ if (!obj) {
+ return obj as Record<K, U>;
+ }
+
return Objects.fromEntries(
Objects.entries(obj).map(([key, value]) => [key, mapper(value)])
);
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index 4cc00f0aa1..ffa96e0fab 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -55,6 +55,14 @@ def _filter_dataframe(
import numpy as np
for _channel, fields in selection.items():
+ # This is a case when altair does not pass back the fields to filter on
+ # and instead passes an individual selected point.
+ if len(fields) == 2 and "vlPoint" in fields and "_vgsid_" in fields:
+ # Vega is 1-indexed, so subtract 1
+ indexes = [int(i) - 1 for i in fields["_vgsid_"]]
+ df = df.iloc[indexes]
+ continue
+
# If vlPoint is in the selection,
# then the selection is a point selection
# otherwise, it is an interval selection
diff --git a/marimo/_plugins/ui/_impl/test_altair_chart.py b/marimo/_plugins/ui/_impl/test_altair_chart.py
index 2f6eb88a13..e5bb7c4bb5 100644
--- a/marimo/_plugins/ui/_impl/test_altair_chart.py
+++ b/marimo/_plugins/ui/_impl/test_altair_chart.py
@@ -26,6 +26,19 @@ def test_filter_dataframe() -> None:
# Filter the DataFrame with the point selection
assert len(_filter_dataframe(df, point_selection)) == 2
+ # Point selected with a no fields
+ point_selection = {
+ "select_point": {
+ "_vgsid_": [2, 3], # vega is 1-indexed
+ "vlPoint": [""],
+ },
+ }
+ # Filter the DataFrame with the point selection
+ assert len(_filter_dataframe(df, point_selection)) == 2
+ first, second = _filter_dataframe(df, point_selection)["field"].values
+ assert first == "value2"
+ assert second == "value3"
+
# Define an interval selection
interval_selection: ChartSelection = {
"signal_channel_2": {"field_2": [1, 3]}