AgGrid Filtering

Hi!

In the example code below I basically have a 3x2 table and a session state list variable with all the names in the table. I’m printing this session state list before and after displaying the AgGrid.

Ok, following this example: we just entered the page and filtered the “Age” column to values greater than 3. Now, the markdown after the list only shows the values “A” & “B”. Of course, the code got executed again from the start so it makes sense. The thing is that the markdown before the table is still seeing “C”, it would need another rerun to not see it.

My goal is to somehow make the code knows a priori (before displaying the AgGrid table) which rows are currently selected on the filter. In other words, when the user filters the table, the code automatically gets run again, so I want to know which rows of the table are “active” (not filtered) before I display the table.

import streamlit as st
import pandas as pd
from st_aggrid import AgGrid

st.set_page_config(page_title = 'test'')

df = pd.DataFrame({'Name': ['A', 'B', 'C'], 'Age': [25, 24, 1]})

if "names_active" not in st.session_state:
    st.session_state["names_active"] = list(df['Name'])
    
st.markdown(st.session_state["names_active"])

return_df = AgGrid(data = df, data_return_mode = 'FILTERED')
st.session_state["names_active"] = list(return_df['data']['Name'])

st.markdown(st.session_state["names_active"])

It may seem a random question but it’s just an example that will solve my problem. Any help would be appreciated! Thanksss!

Anyone with a hint? I’m still struggling with this :sweat_smile:

Do I understand correctly that you want to reset filters after each session?

Actually I want to keep the filters, but I would like to somehow know which rows are selected on the filter before displaying the table.

In the example code there are 2 markdowns (before & after the AgGrid), so when the user filters and the code reruns only the second markdown has the right current selected filtered names, the first one is delayed.

Have you tried using dynamic_filters, there is a post about it. It allows display you dataframe, add filter and the df displayed changes after filters are set.

In case you actually mean “above” and “below”, containers should be able to help you.

container = st.container()

return_df = AgGrid(data = df, data_return_mode = 'FILTERED')
st.session_state["names_active"] = list(return_df['data']['Name'])

with container:
    st.markdown(st.session_state["names_active"])
st.markdown(st.session_state["names_active"])

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.