Hi @thunderbug1
This is my working hypothesis: the issue is neither with Streamlit nor streamlit-aggrid. It has to do with your data. Namely, that with every interaction you generate a completely new DataFrame; wiping your prior selections from memory!
The source of the behavior is: np.random.choice(range(10), size=(100000,3))
Notice that the below example works as expected without st.experimental_memo
:
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
import pandas as pd
import numpy as np
np.random.seed(42)
def get_df():
df = pd.DataFrame(columns=['foo','bar','baz'], data=np.random.choice(range(10), size=(100000,3)))
return df
df = get_df()
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(value=True, enableRowGroup=True, aggFunc=None, editable=False)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
with st.form("table_form", clear_on_submit=False):
grid_response = AgGrid(df, gridOptions=gb.build(), height=700, data_return_mode="AS_INPUT", update_mode="SELECTION_CHANGED")#, enable_enterprise_modules=True)#.style.apply(highlight_clusters, axis=1))
st.write(f"grid_response {grid_response}")
selected = grid_response['selected_rows']
st.write(f"selected {selected}")
if st.form_submit_button("Submit"):
pass
The trick is to make the random data deterministic by setting the random number seed before generating it, via numpy.random.seed(some_number)
.
Caching the data, as you did, effectively did the same thing: made the random data deterministic (as you reuse the cached dataframe with every widget interaction)! When you don’t cache the data and generate random data, hitting the submit button causes the script to run from top-to-bottom… generating a new dataframe without your prior selections.
Hope this helps! Let me know if I’m not being clear.
Best,
Snehan