-
Notifications
You must be signed in to change notification settings - Fork 4k
Description
Checklist
- I have searched the existing issues for similar issues.
- I added a very descriptive title to this issue.
- I have provided sufficient information below to help reproduce this issue.
Summary
When a user selects or more rows in a dataframe and then sorts with a column dropdown menu, the data in the table are reordered but the row selections retain their previous order. Consequently, the returned DataframeState does not match what is shown visually in the frontend!
This only happens if the "sort ascending/descending" option is picked from the dropdown menu. If the column is sorted by clicking on the column name, then the row selections are simply cleared. This is the subject of a seperate feature request: #8851
streamlit-sorting-selection.webm
Reproducible Code Example
import pandas as pd
import streamlit as st
df = pd.DataFrame({"foo": list("ABC")})
state = st.dataframe(df, selection_mode="single-row", on_select="rerun")
rows = state["selection"]["rows"]
if rows:
st.write("You have selected:", df.iat[rows[0], 0])
st.write("DataFrameState:", state)Steps To Reproduce
- Select the top row (A).
- Select the dropdown menu by the column
- Select "Sort Descending"
Observe that the frontend shows that row C is selected, whilst the DataFrameState shows that row A is selected.
Expected Behavior
After the table is sorted, ideally row A should remain selected.
Alternatively, the row selections could be just cleared (though the above behaviour would be preferable).
Current Behavior
The frontend shows that row C is selected. This is incorrect as the user never selected that row, and it's also inconsistent with the DataFrameState which suggests row A is still selected.
Debug info
- Streamlit version: 1.45.0
- Python version: 3.12
- Operating System: Linux
- Browser: Tested on Edge & Chrome
Related: #10701