Remove selected option from Select box

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?

I appreciate any help you can provide.
Binjan

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?

Thanks in advance.

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)

Thank you, this solves my issue.

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