Update options of multiselect widget

I use multiselect widgets to select python functions that I use later in my program. When I select a function, I like to add another one to the options of the multislider. So that I can select the new function in the next “rerun” of the application and chain them together.

Here is a simplified example that does not work like expected. Is there a way to update the multiselect options?

import streamlit as st

if "options" not in st.session_state:
    st.session_state.options = ["a1", "a2", "a3"]

def update_options():
    st.session_state.options.append("a4")

st.multiselect("", st.session_state.options, key="selected", on_change=update_options)

st.write(st.session_state)

Is this what you are looking for?

import streamlit as st
if "options" not in st.session_state:
    st.session_state.options = ["a1", "a2", "a3"]

ti = st.text_input("Enter Function to be added to multiselect")

if ti != "":
    if ti not in st.session_state.options:  # prevent duplicates
        st.session_state.options.append(ti)

st.multiselect("", st.session_state.options, key="selected")
st.write(st.session_state.options)
1 Like

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