Hi @Artemis,
Well, you can do something like this:
import streamlit as st
if "btnval" not in st.session_state: st.session_state.btnval = None
if "mylist" not in st.session_state: st.session_state.mylist = ["a", "b", "c"]
def toggle_btns(vip):
if vip in st.session_state.mylist and st.session_state.btnval == True: st.session_state.mylist.remove(vip)
if vip not in st.session_state.mylist and st.session_state.btnval == False: st.session_state.mylist.append(vip)
st.session_state.txtel = ""
def Callback(): st.session_state.btnval = not st.session_state.btnval
txtip = st.text_input("Element for list", value="", key="txtel", on_change=Callback)
if txtip != "":
st.session_state.btnval = True if txtip in st.session_state.mylist else False
cols = st.columns((2,2,10))
cols[0].button("Add", on_click=toggle_btns, args=(txtip,), disabled=st.session_state.btnval)
cols[1].button("Del", on_click=toggle_btns, args=(txtip,), disabled=not st.session_state.btnval)
st.info(f"Currently mylist contains: {st.session_state.mylist}")
Explanation:
- The list initializes with 3 elements: a, b, c. This can be seen in the blue band at the bottom.
- If you enter any element on the list, you get an option to delete that element
- If you enter any element that is not on the list, you get an option to add that element
You can modify the code to change the logic as you wish.
Cheers