m-wrzr / streamlit-searchbox

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

How to change the value externally via `st.session_state`? #17

Closed samsucik closed 8 months ago

samsucik commented 1 year ago

I'm struggling to make changes to the value displayed in the searchbox. Does anyone have any advice?

What I've tried so far:

While the entry in st.session_state changes, the searchbox continues do display just the default placeholder (Search ...).

Fyi I'm using the 0.1.3 version installed from PyPi

m-wrzr commented 1 year ago

Typically the values displayed are driven by the search_function that is passed to the component. If you want to run it without the search function you can also set the options manually, (the search state is handled via react), See the example below:

key = f"{search.__name__}_manual"
n_options = st.slider("slider", 0, 10, 5, key="slider")

if key in st.session_state:
    st.session_state[key]["options_js"] = [
        {"label": f"{st.session_state[key]['search']}_{i}", "value": i}
        for i in range(n_options)
    ]
    st.session_state[key]["options_py"] = [i for i in range(n_options)]

manual = st_searchbox(
    search_function=lambda _: [],
    key=key,
)

st.write(manual)