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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: support mark_text for altair selection (#4630)
Myles Scolnickec2725af9c97
diff --git a/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap b/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snapindex 5d34c5f13a..6747ff56dd 100644--- a/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap+++ b/frontend/src/plugins/impl/vega/__tests__/__snapshots__/make-selectable.test.ts.snap@@ -1061,6 +1061,22 @@ exports[`makeSelectable > should work for multi-layered charts with different se},{"encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point_1",+ },+ {+ "param": "select_interval_1",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },"text": {"aggregate": "min","field": "temp_min",@@ -1079,12 +1095,61 @@ exports[`makeSelectable > should work for multi-layered charts with different se},"mark": {"align": "right",+ "cursor": "pointer","dx": -5,+ "tooltip": true,"type": "text",},+ "params": [+ {+ "name": "select_point_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval_1",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],},{"encoding": {+ "opacity": {+ "condition": {+ "test": {+ "and": [+ {+ "param": "select_point_2",+ },+ {+ "param": "select_interval_2",+ },+ ],+ },+ "value": 1,+ },+ "value": 0.2,+ },"text": {"aggregate": "max","field": "temp_max",@@ -1103,9 +1168,42 @@ exports[`makeSelectable > should work for multi-layered charts with different se},"mark": {"align": "left",+ "cursor": "pointer","dx": 5,+ "tooltip": true,"type": "text",},+ "params": [+ {+ "name": "select_point_2",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "on": "click[!event.metaKey]",+ "type": "point",+ },+ },+ {+ "name": "select_interval_2",+ "select": {+ "encodings": [+ "x",+ "y",+ ],+ "mark": {+ "fill": "#669EFF",+ "fillOpacity": 0.07,+ "stroke": "#669EFF",+ "strokeOpacity": 0.4,+ },+ "on": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "translate": "[mousedown[!event.metaKey], mouseup] > mousemove[!event.metaKey]",+ "type": "interval",+ },+ },+ ],},],}diff --git a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.tsindex e620160aa6..e2b0e3873d 100644--- a/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts+++ b/frontend/src/plugins/impl/vega/__tests__/make-selectable.test.ts@@ -354,6 +354,10 @@ describe("makeSelectable", () => {"select_point_0","select_interval_0","pan_zoom",+ "select_point_1",+ "select_interval_1",+ "select_point_2",+ "select_interval_2",]);});diff --git a/frontend/src/plugins/impl/vega/make-selectable.ts b/frontend/src/plugins/impl/vega/make-selectable.tsindex 57eab2d036..646897cd47 100644--- a/frontend/src/plugins/impl/vega/make-selectable.ts+++ b/frontend/src/plugins/impl/vega/make-selectable.ts@@ -141,8 +141,8 @@ function makeChartSelectable(return spec;}- // We don't do anything if the mark is text or geoshape- if (mark === "geoshape" || mark === "text") {+ // We don't do anything if the mark is geoshape+ if (mark === "geoshape") {return spec;}@@ -214,15 +214,7 @@ function makeChartInteractive<T extends GenericVegaSpec>(spec: T): T {return spec;}- 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") {+ if (!Marks.isInteractive(spec.mark)) {return spec;}@@ -240,10 +232,10 @@ function makeChartInteractive<T extends GenericVegaSpec>(spec: T): T {function getBestSelectionForMark(mark: Mark): SelectionType[] | undefined {switch (mark) {- case "text":case "arc":case "area":return ["point"];+ case "text":case "bar":return ["point", "interval"];// there is no best selection for linediff --git a/marimo/_smoke_tests/altair/text_selection.py b/marimo/_smoke_tests/altair/text_selection.pynew file mode 100644index 0000000000..e933a3ddf0--- /dev/null+++ b/marimo/_smoke_tests/altair/text_selection.py@@ -0,0 +1,159 @@++# /// script+# requires-python = ">=3.12"+# dependencies = [+# "altair==5.5.0",+# "marimo",+# "vega-datasets==0.9.0",+# ]+# ///++import marimo++__generated_with = "0.13.0"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""## Basic Text""")+ return+++def _(mo):+ from vega_datasets import data+ import altair as alt++ chart = (+ alt.Chart(data.cars())+ .mark_text()+ .encode(+ x="Horsepower",+ y="Miles_per_Gallon",+ text="Origin",+ )+ )++ chart = mo.ui.altair_chart(chart)+ return alt, chart, data+++def _(chart, mo):+ mo.vstack([chart, chart.value.head()])+ return++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""## Bar chart with text""")+ return++[email protected](hide_code=True)+def _(alt, data, mo):+ _source = data.barley()++ bars = (+ alt.Chart(_source)+ .mark_bar()+ .encode(+ x=alt.X("sum(yield):Q").stack("zero"),+ y=alt.Y("variety:N"),+ color=alt.Color("site"),+ )+ )++ text = (+ alt.Chart(_source)+ .mark_text(dx=-15, dy=3, color="white")+ .encode(+ x=alt.X("sum(yield):Q").stack("zero"),+ y=alt.Y("variety:N"),+ detail="site:N",+ text=alt.Text("sum(yield):Q", format=".1f"),+ )+ )++ chart2 = mo.ui.altair_chart(bars + text)+ chart2+ return (chart2,)+++def _(chart2):+ chart2.value+ return++[email protected](hide_code=True)+def _(mo):+ mo.md(+ r"""+ # Bar chart with double text++ This fails for another reason which is that `month(date)` field gets converted into `month_date` and breaks the backend filtering.+ """+ )+ return++[email protected](hide_code=True)+def _(alt, data, mo):+ _source = data.seattle_weather()++ bar = (+ alt.Chart(_source)+ .mark_bar(cornerRadius=10, height=10)+ .encode(+ x=alt.X("min(temp_min):Q")+ .scale(domain=[-15, 45])+ .title("Temperature (°C)"),+ x2="max(temp_max):Q",+ y=alt.Y("month(date):O").title(None),+ )+ )++ text_min = (+ alt.Chart(_source)+ .mark_text(align="right", dx=-5)+ .encode(+ x="min(temp_min):Q", y=alt.Y("month(date):O"), text="min(temp_min):Q"+ )+ )++ text_max = (+ alt.Chart(_source)+ .mark_text(align="left", dx=5)+ .encode(+ x="max(temp_max):Q", y=alt.Y("month(date):O"), text="max(temp_max):Q"+ )+ )++ _chart = (bar + text_min + text_max).properties(+ title=alt.Title(+ text="Temperature variation by month",+ subtitle="Seatle weather, 2012-2015",+ )+ )++ chart3 = mo.ui.altair_chart(_chart)+ chart3+ return (chart3,)+++def _(chart3):+ chart3.selections+ return+++if __name__ == "__main__":+ app.run()