Row Deletion Not Working After Adding a New Entry

I’ve built a streamlit app to display and edit the content of a csv file. I use Aggrid to display the csv as a dataframe, and allow the user to add a line to the dataframe, and delete lines selected with a checkbox. My problem:

  • when the user starts by deleting a line directly, it works
  • when the user first adds a new line and THEN tries to delete a line, it does not work. The app does not recognize that a line (or multiple lines) have been selected with the checkboxes. In order to get this recognized, the user has to select the lines and click “Delete” a second time.

Here is a minimal working example:

import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridUpdateMode, DataReturnMode, GridOptionsBuilder


# Initialize DataFrame in session state for demonstration
if 'df' not in st.session_state:
    st.session_state.df = pd.DataFrame({
        'custom_id': [1, 2, 3],
        'question': ['Q1', 'Q2', 'Q3'],
        'response': ['R1', 'R2', 'R3'],

# Display the grid with AgGrid
grid_table = AgGrid(

st.session_state.df = grid_table['data']

# Button to delete selected rows
if st.button('Delete'):
    selected = grid_table["selected_rows"]
    if selected:
        selected_custom_ids = [row['custom_id'] for row in selected]
        st.session_state.df = st.session_state.df[~st.session_state.df['custom_id'].isin(selected_custom_ids)]
        st.session_state.entry_deleted = True
        st.warning("Please select rows before deleting.")

# Button to toggle the form display
if st.button("Add an entry"):
    st.session_state.show_form = not st.session_state.show_form

# Form to add a new entry
if st.session_state.show_form:
    with st.form("add_entry_form"):
        question = st.text_area("Question", height=30)
        response = st.text_area("Response", height=100)
        synonyms = st.text_area("Synonyms (optional)", height=100)
        submit_button = st.form_submit_button("Add")
        if submit_button:
            if not question or not response:
                st.error("Please fill in all required fields.")
                next_custom_id = st.session_state.df['custom_id'].astype(int).max() + 1
                new_entry = pd.DataFrame({
                    'custom_id': [next_custom_id],
                    'question': [question],
                    'response': [response],
                    'synonyms': [synonyms]
                st.session_state.df = pd.concat([st.session_state.df, new_entry], ignore_index=True)
                st.session_state.show_form = False
                st.session_state.entry_added = True

The problem might be coming from st.rerun(), which I’m using because otherwise the database displayed by AgGrid is not refreshed. But what is going on exactly and how can I fix it?

anyone please? This is the last bug of my app and I need to fix it. :smiley: