m-wrzr / streamlit-searchbox

Streamlit searchbox that dynamically updates and provides a list of suggestions based on a provided function
MIT License
203 stars 26 forks source link

searchbox inside form #20

Closed grudloff closed 9 months ago

grudloff commented 10 months ago

iIs there a way to have a searchbox inside a form? Currently, the suggestion function is called once with the placeholder.

m-wrzr commented 9 months ago

A smooth integration isn't possible as far as I'm aware. The searchbox depends on recurring state updates to re-render the suggestions and the st.form batches these together and only updates on submit.

It is similar to the issue with interdependent widgets described here: https://blog.streamlit.io/introducing-submit-button-and-forms/#limitations-and-other-notes

To get it somewhat working you could make a multi-stage submit flow, with one form button to load suggestions and only do the real submit with the seconds button like this:

with st.form("myform"):
    c1, c2 = st.columns(2)
    with c1:
        sr = st_searchbox(
            search_function=search,
            key=f"{search.__name__}_form",
        )
    with c2:
        st.form_submit_button("load suggestions")

    submit = st.form_submit_button("real submit")
    if submit:
        st.write("form submitted")
        st.write(sr)