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)
You will need at least ‘SELECTION_CHANGED’ is you want to act on / do something further with selected rows.
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.
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.
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.
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…
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.
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")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.