Skip to content

Commit 0c46fca

Browse files
committed
Sort row selection keys
Also, don't use df[col][rownum] in examples--that syntax is still using the pandas index. Instead, an explicit .iloc[...] is needed.
1 parent 34d3dd3 commit 0c46fca

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
Fixed #646: Wrap bare value box value in `<p />` tags. (#668)
1818

19-
Fixed #676: `render.data_frame` was reporting selected rows using 0-based row numbers, but the examples and documentation were unclear about whether these values were row numbers or pandas index values (which default to row numbers, but could easily be something else). Now the examples and documentation are consistent with the behavior. (#677)
19+
Fixed #676: The `render.data_frame` selection feature was underdocumented and buggy (sometimes returning `None` as a row identifier if the pandas data frame's index had gaps in it). With this release, the selection is consistently a tuple of the 0-based row numbers of the selected rows--or `None` if no rows are selected. (#677)
2020

2121
### Other changes
2222

e2e/bugs/0676-row-selection/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ui.p("Select rows in the grid, make sure the selected rows appear below."),
1818
ui.output_data_frame("grid"),
1919
ui.output_table("detail"),
20+
ui.output_text_verbatim("debug"),
2021
class_="p-3",
2122
)
2223

@@ -41,5 +42,10 @@ def detail():
4142
# "split", "records", "index", "columns", "values", "table"
4243
return df.iloc[list(input.grid_selected_rows())]
4344

45+
@output
46+
@render.text
47+
def debug():
48+
return input.grid_selected_rows()
49+
4450

4551
app = App(app_ui, server, debug=True)

e2e/bugs/0676-row-selection/test_0676_row_selection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ def test_row_selection(page: Page, local_app: ShinyAppProc) -> None:
1010
# The purpose of this test is to make sure that the data grid can work on Pandas
1111
# data frames that use an index that is not simply 0-based integers.
1212

13+
row1 = page.locator("#grid tbody tr:nth-child(1)")
1314
row3 = page.locator("#grid tbody tr:nth-child(3)")
1415
result_loc = page.locator("#detail tbody tr:nth-child(1) td:nth-child(1)")
16+
debug_loc = page.locator("#debug")
1517

1618
expect(row3).to_be_visible()
1719
expect(row3.locator("td:nth-child(1)")).to_have_text("three")
20+
expect(debug_loc).to_have_text("()")
1821

1922
expect(result_loc).not_to_be_attached()
2023
row3.click()
2124
expect(result_loc).to_have_text("three")
25+
expect(debug_loc).to_have_text("(2,)")
26+
27+
# Ensure that keys are in sorted order, not the order in which they were selected
28+
row1.click()
29+
expect(debug_loc).to_have_text("(0, 2)")

js/dataframe/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = (props) => {
189189
.keys()
190190
.toList()
191191
.map((key) => parseInt(key))
192+
.sort()
192193
);
193194
}
194195
}

shiny/api-examples/data_frame/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def filtered_df():
6666
# input.summary_data_selected_rows() is a tuple, so we must convert it to list,
6767
# as that's what Pandas requires for indexing.
6868
selected_idx = list(req(input.summary_data_selected_rows()))
69-
countries = summary_df["country"][selected_idx]
69+
countries = summary_df.iloc[selected_idx]["country"]
7070
# Filter data for selected countries
7171
return df[df["country"].isin(countries)]
7272

shiny/www/shared/dataframe/dataframe.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/shared/dataframe/dataframe.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.

0 commit comments

Comments
 (0)