Multiselect based on user input

Hi @Ananya_Roy !

You were close :slight_smile: the thing is, when you press the Submit button, button_sent is set to True and you go inside the if button_sent code block, but any time you interact with the underlying multiselect, the script reruns and since you have not interacted with the button, button_sent variable goes back to None. As a result, you don’t go back inside the multiselect codeblock.

Since you store the info that you clicked the submit button in SessionState, you should use that info to force your code inside the if button_sent code block, something like if button_sent or if button_sent or session_state.button_sent :wink: (the solution is below but hopefully you’re able to correct your code yourself with that info !)

post1.py
import streamlit as st
import SessionState
import naming as n

def inp_det(type):
    if type == 'source':
        st.write('Enter name (Roy/Sen)')
        name = st.text_input('Source name')
    elif type == 'destination':
        st.write('Enter name (Roy/Sen)')
        name = st.text_input('Destination name')
    return name
    
def main():
    name = inp_det('source')
    session_state = SessionState.get(name="", button_sent=False)
    button_sent = st.button("SUBMIT")
    if button_sent or session_state.button_sent: # <-- first time is button interaction, next time use state to go to multiselect
        session_state.button_sent = True
        listnames = n.show_names(name)
        selectednames=st.multiselect('Select your names',listnames)
        st.write(selectednames)

if __name__ == "__main__":
    main()

Fanilo

2 Likes