AgGrid - Selection clears after clicking checkbox

Hello! I’m having an issue with the AgGrid dynamic table. I want users to be able to select multiple rows of a data frame, but when clicking on selectbox, the table reloads and clears the selection. The issue happens from time to time - sometimes I’m able to select 3-4 rows and then after selecting another one, the whole table clears the previous selection. I tried it on the latest versions of the streamlit and aggrid as well as on previous versions. I know that I can change the update mode to manual and it solves the problem but I don’t really want users to click that update button and wondering if there is an easy way to fix this.

Did someone encounter such issue?
Thanks in advance!

Here’s my code:

# define path to excel data source
path = os.path.dirname(__file__)
my_file = path+'/data.xlsx'


# function to load excel data to pandas dataframe
@st.cache(allow_output_mutation=True)
def load_data():
    df = pd.read_excel(my_file)
    return df

#load source data
data = load_data()

gb = GridOptionsBuilder.from_dataframe(data)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gridOptions = gb.build()

response = AgGrid(
    data,
    gridOptions=gridOptions,
    enable_enterprise_modules=False,
    height=600,
    update_mode=GridUpdateMode.SELECTION_CHANGED,
    data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
    fit_columns_on_grid_load=False
)

response_df = pd.DataFrame(response["selected_rows"])
st.dataframe(response_df)
1 Like

Did you try : update_mode=‘MODEL_CHANGED’

I had same issue and used session_state:

if "id_row" not in st.session_state:
    st.session_state["id_row"] = ''
    selected_rows = []
else:
    selected_rows = (list(range(len(st.session_state[id_row].get('selectedRows')))))
gb.configure_selection(selection_mode="multiple", use_checkbox=True,pre_selected_rows=selected_rows)

yes I did try that, unfortunately the same issue.

Hi @Oleksandr_Arsentiev,

  1. You will need at least ‘SELECTION_CHANGED’ is you want to act on / do something further with selected rows.
  2. When selecting AGgrid rows, if you select too fast, your previous selections get sometimes unselected. Try selecting a row and pausing until you see the tick mark, and thereafter select the 2nd row.

Cheers

The session state kind of works but not entirely for my purpose. When I add a key argument to the AgGrid object, for some reason, I cannot filter the underlying dataframe by the other widgets (multiselect boxes)

Hello. Thanks for the reply. I was using ‘SELECTION_CHANGED’ from the very beginning. I think speed of selection is not the case here because the table “resets” even if I click the checkboxes with enough delay.

1 Like

Hei! I’m using the same functionality in different projects, bet somehow in one of them streamlit reloads th e page when checkbox clicked, in other everything works smoothly. I dont understand where is the difference, and why it doesnt work in other app.

1 Like

I am stuck at the same issue. Nothing appears to be working so far.

1 Like

In my case the issue was only during local development, once I deployed the app to Streamlit Cloud, the issue disappeared

1 Like

Anyone have a solution here?

Streamlit latest and streamlit-aggrid latest doesn’t store the selection after rerun. I tried it all with no success.

Not using the cloud, happening both on dev and production envs.

If I use pre_selected_rows the whole app keeps on re-running until something magically stops it.

Can’t figure out what is going on

2 Likes

I had a similar, seemingly random issue with the grid reloading after checking several boxes… I was using a mongodb backend for a dataframe and in the end I discovered that AGGrid kept refreshing the dataframe in the background as I was clicking the checkboxes.

It turns out that I had some entries in the dataframe which were coming back in a random order… and aggrid automatically reloads if it detects any change in the data - causing the selections to be lost. I fixed mine by applying a sort to the dataframe data before passing it to aggrid…

I’m encountering this issue as well. Did anyone solve this yet?

Hi I am also facing same issue can anyone please help!

Hi @kaquisado did you got the solution to it? If yes can you please guide me even?

HI @Gourav haven’t solved it yet but a workaround I did was to not set any key and set reload_data=False. That way the row selection stays. I’ve added st.form that defines the data that flows into the table, when submit is clicked, it completely redraws the table. The redraw is not nice but this way, the row selection does not reset.

2 Likes

I had the same issue and solved it similar to @kaquisado.
(My AG-Grid table has to be updated after every selection change. Based on the selected rows the dataframe changes and the changes then need to be displayed in the AG-Grid table with the same selected rows)

  • reload_date=False
  • either using no key or a key that gets updated before every re-run - I don’t know yet which is the better solution
  • storing the selected_rows list in the session state

Either use a button or form to start the updating of the dataframe and of the selected_rows into the session state.
Or if is updated immediately by changing the selection (update_mode=“selection_changed”), in my case it only worked when I added a little time delay using time.sleep(2). Without this time delay it doesn’t work! As if there is not enough time to save the selection from ag-grid. Does anyone know what could be the reason for this?

Here is part of my working solution:
(streamlit==1.20.0, streamlit-aggrid==0.3.3)
very important: it doesn’t work with the latest streamlit-aggrid version 0.3.4! There is some issue with the pre_selected_rows

st.write("AG-Grid with pre-selected rows:")
gb = GridOptionsBuilder.from_dataframe(st.session_state.example_df)
gb.configure_selection("multiple", use_checkbox=True, pre_selected_rows=st.session_state.selected_rows)
gridOptions = gb.build()

ag_grid = AgGrid(
    st.session_state.example_df,
    gridOptions=gridOptions,
    data_return_mode="as_input",
    update_mode="selection_changed",
    fit_columns_on_grid_load=True,
    allow_unsafe_jscode=True,
    enable_enterprise_modules=False,
    reload_data=False,
)
placeholder_sessstate = st.empty()
placeholder_sessstate.write("Session State of selected rows:  " + str(st.session_state.selected_rows))

temp1 = []
ag_selected_rows_list = ag_grid["selected_rows"]
for selected_row_dict in ag_selected_rows_list:
    temp1.append(selected_row_dict["_selectedRowNodeInfo"]["nodeRowIndex"])
time.sleep(2)
if st.session_state.selected_rows != temp1:
    st.session_state.selected_rows = temp1
    placeholder_sessstate.write("Session State of selected rows:  " + str(st.session_state.selected_rows))
    
    # Function that updates the dataframe according to selected rows in session state
    update_dataframe(st.session_state.example_df) #

    st.experimental_rerun()
    #st_autorefresh(interval=((500)), key="dataframerefresh")