Skip to content

Commit 437793a

Browse files
committed
Restore input.<ID>_selected_rows(). Add tests
1 parent c8ed2c3 commit 437793a

File tree

8 files changed

+79
-6
lines changed

8 files changed

+79
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
* Removed temporary state where a data frame renderer would try to subset to selected rows that did not exist. (#1351)
2323

24+
* Restored `@render.data_frame`'s (prematurely removed) input value `input.<ID>_selected_rows()`. This value is to be considered deprecated. Please use `<ID>.input_cell_selection()["rows"]` moving forward. (#1345)
25+
2426
### Other changes
2527

2628
* `Session` is now an abstract base class, and `AppSession` is a concrete subclass of it. Also, `ExpressMockSession` has been renamed `ExpressStubSession` and is a concrete subclass of `Session`. (#1331)

js/data-frame/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = ({
364364
columnFilters,
365365
]);
366366

367+
// Restored for legacy purposes. Only send selected rows to Shiny when row selection is performed.
368+
useEffect(() => {
369+
if (!id) return;
370+
let shinyValue: number[] | null = null;
371+
if (rowSelectionModes.row !== SelectionModes._rowEnum.NONE) {
372+
const rowSelectionKeys = rowSelection.keys().toList();
373+
const rowsById = table.getSortedRowModel().rowsById;
374+
shinyValue = rowSelectionKeys.map((key) => rowsById[key].index).sort();
375+
}
376+
Shiny.setInputValue!(`${id}_selected_rows`, shinyValue);
377+
}, [id, rowSelection, rowSelectionModes, table]);
378+
367379
// ### End row selection ############################################################
368380

369381
// ### Editable cells ###############################################################

shiny/www/shared/py-shiny/data-frame/data-frame.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/shared/py-shiny/data-frame/data-frame.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pandas as pd
2+
3+
from shiny import App, Inputs, reactive, render, req, ui
4+
5+
app_ui = ui.page_fluid(
6+
ui.output_data_frame("df1"),
7+
ui.output_text_verbatim("selected_rows", placeholder=True),
8+
ui.output_text_verbatim("cell_selection", placeholder=True),
9+
)
10+
11+
12+
def server(input: Inputs):
13+
df = reactive.Value(pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns=["A", "B"]))
14+
15+
@render.data_frame
16+
def df1():
17+
return render.DataGrid(df(), selection_mode="rows")
18+
19+
@render.text
20+
def selected_rows():
21+
return f"Input selected rows: {input.df1_selected_rows()}"
22+
23+
@render.text
24+
def cell_selection():
25+
cell_selection = df1.input_cell_selection()
26+
if cell_selection is None:
27+
req(cell_selection)
28+
raise ValueError("Cell selection is None")
29+
if cell_selection["type"] != "row":
30+
raise ValueError(
31+
f"Cell selection type is not 'row': {cell_selection['type']}"
32+
)
33+
rows = cell_selection["rows"]
34+
return f"Cell selection rows: {rows}"
35+
36+
37+
app = App(app_ui, server)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from __future__ import annotations
2+
3+
from conftest import ShinyAppProc
4+
from controls import OutputDataFrame, OutputTextVerbatim
5+
from playwright.sync_api import Page
6+
7+
8+
def test_row_selection(page: Page, local_app: ShinyAppProc) -> None:
9+
page.goto(local_app.url)
10+
11+
df = OutputDataFrame(page, "df1")
12+
selected_rows = OutputTextVerbatim(page, "selected_rows")
13+
cell_selection = OutputTextVerbatim(page, "cell_selection")
14+
15+
df.expect_n_row(3)
16+
selected_rows.expect_value("Input selected rows: ()")
17+
cell_selection.expect_value("Cell selection rows: ()")
18+
19+
df.select_rows([0, 2])
20+
21+
selected_rows.expect_value("Input selected rows: (0, 2)")
22+
cell_selection.expect_value("Cell selection rows: (0, 2)")

0 commit comments

Comments
 (0)