Update .csv file with a callback


I am creating a program that:

  1. reads a .csv file
  2. displays it as an editable Dash AG Grid
  3. the file is updated after the push of a button

Is there a way to achieve the 3rd step? I found a way to save a table. but this downloads the table as .csv and not update the existing one.

update_message :  id for html.Div 
update_table: id for a button
editable_grid: id of Dash AG Grid in question
data_storage: id of dcc.Store (stored the filepath in it)

@app.callback(Output('update_message', 'children'),
              Input('update_table', 'n_clicks'),
              State('editable_grid', 'rowData'),
              State('data_storage', 'data'),

def save_changes(n_clicks, grid_data, filename):
    if n_clicks > 0:
        # Convert ag-Grid data back to a DataFrame
        df = pd.DataFrame(grid_data)
        # Save the DataFrame back to the CSV file (you can choose your file name)
        df.to_csv(filename, index=False)
        # Display a confirmation message
        return "File updated successfully"