Repository navigation
feat(plotly): selected()/brushed() are state accessors, seeded on bridge bind #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpsievert
wants to merge
10
commits into
feat/eager-state-accessors
Choose a base branch
from
spike/plotly-selection-state
base: feat/eager-state-accessors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
805f1ae
Seed plotly selection state on bridge bind; make selected()/brushed()…
cpsievert 4fffd7d
Playwright regression test: selection before first read of plotly sta…
cpsievert 2efc4f1
Review fixes: seed implicit coords and empty selections; ungated fixt…
cpsievert 9e1f43d
Merge remote-tracking branch 'origin/feat/eager-state-accessors' into…
cpsievert 2725724
Seed typed-array selectedpoints; skip calcdata fallback for histograms
cpsievert 0e72bac
Seed review round 3: scattergl coords, date-axis lassos, subplot refs
cpsievert 4817e5f
Stop lasso vertex captures before the closing Z command
cpsievert dcd88f0
Seed review round 5: multicategory coords, trim lasso vertex whitespace
cpsievert aae98bc
Detect multicategory axes structurally, not per selected point
cpsievert 7e95d60
Seed a preconfigured empty selection as points: []
cpsievert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pandas as pd | ||
| import plotly.express as px | ||
| from shiny import App, reactive, render, ui | ||
| from shinywidgets import output_widget, render_plotly | ||
|
|
||
| # The accessor reads the tests assert on are gated behind an action button, | ||
| # and NOTHING reads them at startup (an ungated read would attach the | ||
| # accessor early, defeating the scenario). A selection made before the | ||
| # button click exercises the eager state attach and the seed on bridge bind | ||
| # (see the tests). | ||
|
|
||
| df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [1, 4, 9, 16, 25], "g": list("aabbb")}) | ||
|
|
||
| app_ui = ui.page_fluid( | ||
| ui.input_action_button("read_selection", "Read selection"), | ||
| ui.output_text("selected"), | ||
| ui.output_text("selected_rows"), | ||
| ui.output_text("brushed"), | ||
| ui.card(output_widget("plot"), height="450px"), | ||
| ) | ||
|
|
||
|
|
||
| def server(input, output, session): | ||
| @render_plotly | ||
| def plot(): | ||
| fig = px.scatter(df, x="x", y="y", color="g", custom_data=[df.index]) | ||
| fig.update_traces(marker_size=14) | ||
| fig.update_layout(dragmode="select") | ||
| return fig | ||
|
|
||
| @render.text | ||
| @reactive.event(input.read_selection) | ||
| def selected(): | ||
| pts = plot.selected() | ||
| if pts is None: | ||
| return "None" | ||
| return ";".join(f"{r.trace_index},{r.point_index}" for r in pts.itertuples()) | ||
|
|
||
| @render.text | ||
| @reactive.event(input.read_selection) | ||
| def selected_rows(): | ||
| return ",".join(str(x) for x in plot.selected_rows(df)["x"]) | ||
|
|
||
| @render.text | ||
| @reactive.event(input.read_selection) | ||
| def brushed(): | ||
| b = plot.brushed() | ||
| return "None" if b is None else ",".join(sorted(b)) | ||
|
|
||
|
|
||
| app = App(app_ui, server) |
136 changes: 136 additions & 0 deletions
136
tests/playwright/plotly_selection_seed/test_plotly_selection_seed.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from playwright.sync_api import Page, expect | ||
| from playwright.sync_api import TimeoutError as PlaywrightTimeoutError | ||
| from tests.playwright.conftest import assert_no_unexpected_errors, collect_errors | ||
|
|
||
| # Page coordinates of a data point, via plotly.js's axis conversions (same | ||
| # helper as tests/playwright/plotly_accessors). | ||
| POINT_JS = """([id, x, y]) => { | ||
| const gd = document.querySelector(`#${id} .js-plotly-plot`); | ||
| const L = gd._fullLayout; | ||
| const box = gd.querySelector(".nsewdrag").getBoundingClientRect(); | ||
| return [box.left + L.xaxis.d2p(x), box.top + L.yaxis.d2p(y)]; | ||
| }""" | ||
|
|
||
| # True once the plot holds a selection (trace selectedpoints is also where | ||
| # the bridge's seed reads it from). Confirms a drag gesture actually landed: | ||
| # the plot can be visible before it accepts drags. | ||
| SELECTED_STATE_JS = """(id) => { | ||
| const gd = document.querySelector(`#${id} .js-plotly-plot`); | ||
| if (!gd || !gd._fullLayout) return false; | ||
| return (gd.data || []).some( | ||
| (t) => Array.isArray(t.selectedpoints) && t.selectedpoints.length > 0 | ||
| ); | ||
| }""" | ||
|
|
||
| DESELECTED_STATE_JS = SELECTED_STATE_JS.replace("return (", "return !(") | ||
|
|
||
|
|
||
| def wait_for_sync(page: Page) -> None: | ||
| """Wait until a selection/deselection made in the browser has reached | ||
| the server, WITHOUT reading an accessor (an ungated accessor read would | ||
| attach it early, defeating the late-first-read scenario). plotly.js | ||
| emits plotly_selected a few hundred ms after mouseup (held back for | ||
| double-click disambiguation), and client->server messages are ordered, | ||
| so once the delay has passed, the next message (the button click) is | ||
| processed after the selection message.""" | ||
| page.wait_for_timeout(700) | ||
|
|
||
|
|
||
| def point(page: Page, x: float, y: float, id: str = "plot") -> tuple[float, float]: | ||
| px, py = page.evaluate(POINT_JS, [id, x, y]) | ||
| return px, py | ||
|
|
||
|
|
||
| def drag(page: Page, start: tuple[float, float], end: tuple[float, float]) -> None: | ||
| page.mouse.move(*start) | ||
| page.mouse.down() | ||
| page.mouse.move((start[0] + end[0]) / 2, (start[1] + end[1]) / 2, steps=5) | ||
| page.mouse.move(*end, steps=5) | ||
| page.mouse.up() | ||
|
|
||
|
|
||
| # The three points of df with x in [1.5, 4.5]: trace 0's point 1 and trace | ||
| # 1's points 0 and 1 (df rows 1, 2, 3) | ||
| SELECTED_POINTS = "0,1;1,0;1,1" | ||
| SELECTED_ROWS = "2,3,4" | ||
| SELECTED_COUNT = "3" | ||
|
|
||
|
|
||
| def box_select(page: Page, id: str = "plot") -> None: | ||
| """Box-select the points with x in [1.5, 4.5], retrying until the | ||
| selection lands on the graph div.""" | ||
| for _ in range(10): | ||
| drag(page, point(page, 1.5, 26, id=id), point(page, 4.5, 0, id=id)) | ||
| try: | ||
| page.wait_for_function(SELECTED_STATE_JS, arg=id, timeout=3000) | ||
| return | ||
| except PlaywrightTimeoutError: | ||
| pass | ||
| page.wait_for_function(SELECTED_STATE_JS, arg=id, timeout=30000) | ||
|
|
||
|
|
||
| def goto_plot(page: Page, local_app) -> None: | ||
| page.goto(local_app.url) | ||
| expect(page.locator("#plot .js-plotly-plot")).to_be_visible(timeout=30000) | ||
| # Nothing reads the accessors until the button is clicked (any ungated | ||
| # read would attach them early), so the gated outputs are still empty | ||
| expect(page.locator("#selected")).to_have_text("") | ||
| expect(page.locator("#selected_rows")).to_have_text("") | ||
| expect(page.locator("#brushed")).to_have_text("") | ||
|
|
||
|
|
||
| def test_selection_before_first_read_is_reported(page: Page, local_app) -> None: | ||
| """A box selection made before the accessor's (gated) first read is | ||
| reflected in that read. Nothing in the app reads the accessors before | ||
| the button click, so the first read is genuinely late: pre-change | ||
| (kind="event", lazy attach on first read) the selection below fired | ||
| into a nonexistent listener and this read returned None. | ||
|
|
||
| selected()/brushed() are kind="state" accessors: they attach at render, | ||
| and the bridge seeds the plot's current selection when the bind reaches | ||
| the browser (js/src/plotly.ts), so the selection is captured whether it | ||
| preceded the bind (seed) or followed it (live forwarding). | ||
| """ | ||
| errors, failed_requests = collect_errors(page) | ||
| goto_plot(page, local_app) | ||
|
|
||
| # Select BEFORE the gated first read of selected()/selected_rows()/ | ||
| # brushed(), and wait for the selection to reach the server before | ||
| # clicking (see wait_for_sync) | ||
| box_select(page) | ||
| wait_for_sync(page) | ||
|
|
||
| page.locator("#read_selection").click() | ||
| expect(page.locator("#selected")).to_have_text(SELECTED_POINTS, timeout=30000) | ||
| expect(page.locator("#selected_rows")).to_have_text(SELECTED_ROWS) | ||
| expect(page.locator("#brushed")).to_have_text("range") | ||
|
|
||
| assert_no_unexpected_errors(errors, failed_requests) | ||
|
|
||
|
|
||
| def test_deselect_after_read_clears_selection(page: Page, local_app) -> None: | ||
| """A deselect (double-click on the plot background) after the gated read | ||
| clears the accessors: a re-read reports no selection.""" | ||
| errors, failed_requests = collect_errors(page) | ||
| goto_plot(page, local_app) | ||
|
|
||
| box_select(page) | ||
| wait_for_sync(page) | ||
| page.locator("#read_selection").click() | ||
| expect(page.locator("#selected")).to_have_text(SELECTED_POINTS, timeout=30000) | ||
| expect(page.locator("#brushed")).to_have_text("range") | ||
|
|
||
| # Double-click the plot background (x=1, y=25 is no data point), wait | ||
| # for the deselect to reach the server, then re-read: cleared | ||
| page.mouse.dblclick(*point(page, 1, 25)) | ||
| page.wait_for_function(DESELECTED_STATE_JS, arg="plot", timeout=30000) | ||
| wait_for_sync(page) | ||
|
|
||
| page.locator("#read_selection").click() | ||
| expect(page.locator("#selected")).to_have_text("None", timeout=30000) | ||
| expect(page.locator("#selected_rows")).to_have_text("") | ||
| expect(page.locator("#brushed")).to_have_text("None") | ||
|
|
||
| assert_no_unexpected_errors(errors, failed_requests) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.