Hi all,
I am using Select Box on my interface.
I want to remove the selected option on the button click.
my selectbox st.selectbox("Select Question:", options=csv(), key="select_question", index=None, placeholder="Select Question...", disabled=btn_disabled)
I have tried select_box.index = None
but, this is not working.
Can someone help me remove the selected option in Select Box?
Hello !
To remove the selected option from the select box I believe you need to rerun the code that defines it.
I made a working example using the st.rerun command in combination with the fragment decorator to avoid reruning your entire app !
Tell me if this works for you and if you have further questions !
Here is the code :
import streamlit as st
@st.fragment
def fragment():
options = [str(i) for i in range(10)]
btn_disabled = False
if "select_question" not in st.session_state:
st.session_state["select_question"] = None
st.selectbox(
"Select Question:",
options=options,
key="select_question",
placeholder="Select Question...",
disabled=btn_disabled,
)
st.markdown(st.session_state["select_question"])
if st.button("Reset option in selectbox"):
del st.session_state["select_question"]
st.rerun(scope="fragment")
fragment()
Thank you for the solution.
I cannot convert my design with fragment option.
I have a button with on_click callback. st.button("Clear", key="btn_clear", on_click=btn_clear_click, disabled=btn_disabled)
Below is my design code:
# different action buttons
with st.container():
# help text
st.write(f"""**Please enter the specific information you're seeking from the Customer Contracts.
Please rate the response using the :material/thumb_up: or :material/thumb_down: buttons, and use the 'Feedback' button to provide additional feedback.**""")
( col_customer, col_question )= st.columns([4, 8], vertical_alignment="center")
col_customer.selectbox("Select Customer:", options=get_customers(), key="select_customer", index=None, placeholder="Select Customer...", disabled=btn_disabled)
col_question.selectbox("Select Question:", options=read_csv(), key="select_question", index=None, placeholder="Select Question...", disabled=btn_disabled)
# different action buttons
with st.container():
( col_clear, col_answer, col_answer_relevancy,
col_faithfulness_relevancy, col_feedback,
col_feedback_report )= st.columns([1, 3, 2.5, 2.5, 1.5, 1.5], vertical_alignment="center", gap="small")
# clear and answer button
col_clear.button("Clear", key="btn_clear", on_click=btn_clear_click, disabled=btn_disabled)
Is there any other alternative to clearing the select box?
If you’re using btn_clear_click in a callback, your Streamlit app will rerun so you don’t need to call a rerun manually. Here is a working example :
import streamlit as st
st.set_page_config(layout="wide")
def get_customers():
return list("12345")
def read_csv():
return list("QWERT")
btn_disabled=False
def btn_clear_click():
del st.session_state["select_question"]
del st.session_state["select_customer"]
return
# different action buttons
with st.container():
# help text
st.write(f"""**Please enter the specific information you're seeking from the Customer Contracts.
Please rate the response using the :material/thumb_up: or :material/thumb_down: buttons, and use the 'Feedback' button to provide additional feedback.**""")
( col_customer, col_question )= st.columns([4, 8], vertical_alignment="center")
if "select_question" not in st.session_state:
st.session_state["select_question"] = None
if "select_customer" not in st.session_state:
st.session_state["select_customer"] = None
col_customer.selectbox("Select Customer:", options=get_customers(), key="select_customer", index=None, placeholder="Select Customer...", disabled=btn_disabled)
col_question.selectbox("Select Question:", options=read_csv(), key="select_question", index=None, placeholder="Select Question...", disabled=btn_disabled)
# different action buttons
with st.container():
( col_clear, col_answer, col_answer_relevancy,
col_faithfulness_relevancy, col_feedback,
col_feedback_report )= st.columns([1, 3, 2.5, 2.5, 1.5, 1.5], vertical_alignment="center", gap="small")
# clear and answer button
col_clear.button("Clear", key="btn_clear", on_click=btn_clear_click, disabled=btn_disabled)
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.