PablocFonseca / streamlit-aggrid

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

checkbox deselected issue with key and reload_data=True #188

Closed smartdolphin closed 3 months ago

smartdolphin commented 1 year ago

If you configure the Streamlit aggrid to reload data with key and reload_data=True, the checkbox will become unchecked when clicked. What might be the cause of this behavior?

Here's the sample code for reference:

version

streamlit                         1.17.0
streamlit-aggrid                  0.3.3

code

import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
from st_aggrid.shared import DataReturnMode, GridUpdateMode, JsCode

def show_grid(
    df: pd.DataFrame,
    reload_data: bool = False,
    key: str = "",
):
    if f"{key}_row" not in st.session_state:
        selected_rows = []
    else:
        selected_rows = st.session_state[f"{key}_row"]

    gb = GridOptionsBuilder.from_dataframe(df)
    gb.configure_selection(
        "single",
        use_checkbox=True,
        pre_selected_rows=selected_rows,
    )
    grid_options = gb.build()
    grid = AgGrid(
        df,
        gridOptions=grid_options,
        height=400,
        fit_columns_on_grid_load=True,
        configure_side_bar=False,
        theme="balham", # streamlit, alpine, balham, material
        update_mode=GridUpdateMode.SELECTION_CHANGED,
        data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
        allow_unsafe_jscode=True,
        reload_data=reload_data,
        key=key,
    )
    if len(grid["selected_rows"]) > 0:
        st.session_state[f"{key}_row"] = [
            row["_selectedRowNodeInfo"]["nodeRowIndex"] for row in grid["selected_rows"]
        ]

if __name__ == "__main__":
    df = pd.read_excel("test.xlsx")
    show_grid(df, reload_data=True, key="unique")
elcronos commented 1 year ago

Have you been able to solve this @smartdolphin ? I have the same issue