Deleting containers with buttons only deletes the last one

This function call and button only deletes the last condition no matter which button you press.
This app is locally deployed so I cannot share the link.

def delete_cond(i):
    st.session_state['entry_conditions'].pop(i)
    print(f'popping index {i}')

for i, condition in enumerate(st.session_state.entry_conditions):
    cols = st.columns([5, 1])
    with cols[0]:
        st.session_state.entry_conditions[i] = st_tags(
            label=f"Entry Condition {i+1}",
            suggestions=suggests,
            text="",
            value=st.session_state.entry_conditions[i],
            key=f"entry_condition_{i}"
        )
    with cols[1]:
        st.button(f"Remove condition {i}", on_click=delete_cond, args=[i])

Even if i create a special button under all the conditions meant specifically to delete the first condition (or any other), it still wont delete the one I want, and will only delete the last one.

Hi @Kabir,

The problem comes down to this – every time the streamlit app runs, it’s running through all of the entry conditions, and re-creating the tags components with the same keys they had before. So, the st_tags with key entry_condition_0 will always be the first entry, and unless you force that st_tags to be totally re-created (specifically by changing that key), it will persist in having the same values it did before.

Maybe more helpful, here’s an example that uses a dictionary instead of a list for st.session_state.entry_conditions, and uses the keys of the dictionary to make sure that specific entry gets deleted, and using those same keys (rather than just enumerate) to set the keys for each widget.

Here I’m using st.multiselect instead of st_tags as it’s a bit simpler to get working:

import streamlit as st

if "entry_conditions" not in st.session_state:
    st.session_state["entry_conditions"] = {
        0: ["a"],
        1: ["b"],
        2: ["c"],
        3: ["d"],
    }

options = ["a", "b", "c", "d", "e", "f"]


def delete_cond(i):
    del st.session_state["entry_conditions"][i]
    st.toast(f"popping index {i}")


for key, value in st.session_state["entry_conditions"].items():
    cols = st.columns([5, 1])
    with cols[0]:
        st.session_state["entry_conditions"][key] = st.multiselect(
            label=f"Entry Condition {key}",
            options=options,
            key=f"entry_condition_{key}",
            default=value,
        )
    with cols[1]:
        st.button(f"Remove condition {key}", on_click=delete_cond, args=(key,))

Here’s a playground showing it in action.

1 Like

Good answer to a subtle bug.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.