So I’m making a streamlit application (an unsubscribe feature) and I want my users to be able to select the rows they want to unsubscribe from, then click a button to confirm.
I have a small working example below:
import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridUpdateMode, DataReturnMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
def make_aggrid_table(data: pd.DataFrame, key: str) -> pd.DataFrame:
"""
The function that creates the aggrid table. taken from the aggrid_example.py file.
:param: data: dataframe of the interaction data
:return: dataframe of the selected rows from the aggrid table
"""
gb = GridOptionsBuilder.from_dataframe(data, min_column_width=100)
gb.configure_default_column(wrapText=True, autoHeight=True)
gb.configure_selection('multiple', use_checkbox=True,
groupSelectsChildren="Group checkbox select children") # Enable multi-row selection
gb.configure_side_bar() # Add a sidebar
grid_options = gb.build()
grid_response = AgGrid(
data,
gridOptions=grid_options,
data_return_mode=DataReturnMode.AS_INPUT,
update_mode=GridUpdateMode.MODEL_CHANGED,
fit_columns_on_grid_load=True,
theme='dark', # Add theme color to the table
enable_enterprise_modules=True,
height=370,
reload_data=False,
wrap_text=True,
resizeable=True,
key=key
)
grid_response_data = grid_response['data']
selected = grid_response['selected_rows']
df = pd.DataFrame(selected) # Pass the selected rows to a new dataframe df
return df
pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, index=range(3)).to_csv('some_file.csv')
some_csv = pd.read_csv('some_file.csv', index_col=0)
selection = make_aggrid_table(some_csv, 'some_key')
if not selection.empty:
print(selection)
delete = st.button('Delete')
if delete:
for index, row in selection.iterrows():
some_csv.drop(index, inplace=True, axis=0)
some_csv.to_csv('some_file.csv')
At initialization, all looks good, the app has all three rows and all three entries in the CSV (pictured below)
but after I press delete, the CSV changes, but my view of the data does not.
(pictured below)
This is causing issues with users ‘double unsubscribing’. How should I structure my code so that the view changes after the button is pressed?