I am trying the multipage method. On the first page I have an ag-grid component. On selecting a row I display further information. If I navigate to another page (actually there is a button and code which will transfer selected data to another page) and come back, the first page has been rerun and the table is back to an unselected state.
I’d like to capture the selected row and restore it - using either st.session_state or my own state machine.
import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
for k, v in st.session_state.items():
st.session_state[k] = v
df = pd.DataFrame(
{
"index": [0, 1, 2],
"col1": [1, 2, 3],
"col2": [2, 3, 4],
}
).set_index("index")
gb = GridOptionsBuilder.from_dataframe(df)
try:
selected_row = st.session_state["table_key"]["selectedItems"][0]["rowIndex"]
except (KeyError, IndexError):
selected_row = 0
gb.configure_selection("single", pre_selected_rows=[selected_row])
grid_return = AgGrid(
df, gridOptions=gb.build(), fit_columns_on_grid_load=True, key="table_key"
)
And also put the following at the top of each page in the app (this fixes an issue where the automatic session state entry associated with the table’s key doesn’t get removed when you switch pages)
for k, v in st.session_state.items():
st.session_state[k] = v
Unfortunately, the suggested solution does not work if the dataframe contains random values. I have opened a new thread to discuss this issue, which you can find here:
Oh, you helped me a lot!
A key to my problem was that row numbers in pre-selection must be STRINGS str(number) ! This does the trick! But WHY??? WHY it’s not in the docs?
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.