Update the value of metric in streamlit using session_state

I have streamlit app where i load a dataframe with the ability to filter and return data based on the user input. I have a function that return the number of returned records using df.shape[0].

What i want is to change the displayed number of records based on the return result by the user filter. so i did use for the st.session_state but it seems i misused.


    def metric_employee(df):
        if "df" in st.session_state:
            return df['status_type'].value_counts()["employee"]
        st.session_state.df=pd.DataFrame(df,columns= df.columns)
        return st.session_state.df

    result = df    
    col1_m,col2_m,col3_m = st.columns(3)
        #  use session state to display number corresponding to the result table!!!!!!!!
        with col1_m:
                f'<p class = "employee_metric"> {metric_employee(result)} employee</p>',
                unsafe_allow_html= True,

    if radioDB=="Search":  
            regular_search_term=st.sidebar.text_input("Enter Search Term")
            if st.sidebar.button("search",key=1):    
                result = df[df['name'].str.contains(regular_search_term)|

                st.write(f"{str(result.shape[0])} Records ")

st.write(f"{str(result.shape[0])} Records ")

the statement above display the total returned result
what i want in my metric is to display the number of records in column status_type where the cell value is employee , and i want to change this number each time the user filter the dataframe.

so what is my mistake and how to change the number based on the returned results?

I think you need to provide a longer working example to clarify what you are trying to do here, at the moment it seems to me that if df is in session_state you will not use the cached df at all, otherwise you will replace it? In either cases, what you are returning is the df which is being defined in the rest of the code, not the one previously saved in st.session_state.

i will edit my question and add more code in order to explain what i want