PablocFonseca / streamlit-aggrid

Implementation of Ag-Grid component for Streamlit
https://pypi.org/project/streamlit-aggrid/
MIT License
1.05k stars 199 forks source link

Grid loses selection in UI, while still having state in the backend #216

Open mattinbits opened 1 year ago

mattinbits commented 1 year ago

I have a use case where I have two multi-select AGGrids on a page. When selecting different rows across the two grids, occasionally one grid will stop showing selected rows in the UI, although it will still maintain the knowledge of what was selected in the backend, so it ends up like this:

image

In the screenshot, the lower grid shows no selected rows, but below the grid I am printing the selected_rows, that shows which rows were selected just before the UI glitched.

It can take quite a lot of selecting and de-selecting of rows across both grids to trigger the issue when using the minimal example below. In the more complicated app where I discovered the issue it was happening much more frequently.

streamlit version: 1.22.0 streamlit-aggrid version: 0.3.4.post3

import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode
import pandas as pd

base_df = pd.DataFrame({
    "id": range(2, 7),
    "char_id": [145233 for _ in range(5)],
    "name": ["Gandalf" for _ in range(5)],
    "location": ["Middle Earth" for _ in range(5)],
    "tome": ["Lord of the rings" for _ in range(5)],
    "chapter": ["Fellowship of the ring" for _ in range(5)],
    "value_1": ["absddsfsdfsdf" for _ in range(5)],
    "value_2": ["both" for _ in range(5)],
    "previous_schedule_id": ["scheduled__2023-04-26T04:00:00+00:00" for _ in range(5)],
    "new_schedule_id": ["scheduled__2023-04-28T04:00:00+00:00" for _ in range(5)],
    "ts": [
        1682943993698,
        1682943993698,
        1682943993698,
        1682943993698,
        1682943993698
    ]
})

df_1 = base_df.drop("value_1", axis=1)

gdb1 = GridOptionsBuilder.from_dataframe(df_1)
gdb1.configure_selection(selection_mode="multiple")
grid_opt1 = gdb1.build()
t1 = AgGrid(
    df_1,
    gridOptions=grid_opt1,
    update_mode=GridUpdateMode.SELECTION_CHANGED,
)

selected_1 = t1.selected_rows if t1 else None
for r in selected_1:
    st.text(r)

df_2 = base_df.drop("value_2", axis=1)

gdb2 = GridOptionsBuilder.from_dataframe(df_2)
gdb2.configure_selection(selection_mode="multiple")
grid_opt2 = gdb2.build()
t2 = AgGrid(
    df_2,
    gridOptions=grid_opt2,
    update_mode=GridUpdateMode.SELECTION_CHANGED,
)

selected_2 = t2.selected_rows if t2 else None
for r in selected_2:
    st.text(r)