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.
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,))
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.