Dynamic filters and st.data_editor

Hi everyone,
I’m working on a streamlit web app where the user has to upload a CSV file and after he can filter it and edit/delete rows.
I’m using the streamlit_dynamic_filters component for the filter part, but what I’m not able to achieve is the following:
When the user filters the st.data_editor and edits a value or delete a row, it is not actually edited, because when I remove the filter the original dataframe (so not edited) gets displayed.

For simplicity I’ll use a dummy dataset:

import streamlit as st
import pandas as pd
from streamlit_dynamic_filters import DynamicFilters


#df = st.file_uploader("Carica il file csv", type=[
                      #'csv'], label_visibility="visible")

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma', 'Frank', 'Grace', 'Hannah', 'Ian', 'Julia'],
    'Surname': ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez'],
    'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70]}

df = pd.DataFrame(data)

if "dfa" not in st.session_state:
    st.session_state["dfa"] = df
dynamic_filters = DynamicFilters(st.session_state["dfa"], filters=['Name','Age'], filters_name='my_filters')

new_df = dynamic_filters.filter_df()



I know I should probably work with the session states but the thing is, when for example I edit or delete a row, in [“edited_rows”] and [“deleted_rows”] I have the indexes of the data_editor which are different from the indexes of st.session_state[“dfa”]

Maybe you guys @Oleksandr_Arsentiev @blackary could help me out :frowning:

Hi @saasyp I see what the issue is but unfortunately not sure if there is an easy fix/workaround. The dynamic filters component was built to work with st.dataframe. I think you’re right in saying that session state could potentially solve it.

Maybe try something like this, passing edited df to DynamicFilters:

df = pd.DataFrame(data)

if 'edited_df' not in st.session_state:
    dynamic_filters = DynamicFilters(df, filters=['Name', 'Age'], filters_name='my_filters')
    dynamic_filters = DynamicFilters(st.session_state['edited_df'], filters=['Name', 'Age'], filters_name='my_filters')

dynamic_filters.display_filters(location='columns', num_columns=2)

# filtered df
new_df = dynamic_filters.filter_df()

st.session_state['edited_df'] = st.data_editor(new_df, num_rows='dynamic', key='my_key', hide_index=False, use_container_width=True,
               column_order=('Name', 'Surname', 'Age'))

Thanks for the help, but unfortunately your solution doesn’t work because when I filter then I edit a cell the filtered dataframe will become the data_editor that I will display, so if for instance one row will be filtered this row will be my next data_editor losing all the other rows.