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: