Multiselect based on user input

Hi team,
I am trying to get a multiselect option based on certain inputs from the user. The list which I need to display is not deterministic. I have pasted the sample codes here.

naming.py --------------------> sample code to get a non-deterministic list

def show_names(name):
	if name == "Roy":
		list1 = ['Riya','Ananya','Tanya','Sanya','Nikita']
	elif name == "Sen":
		list1 = ['Tarun','Arun','Bob','Karan','John']
	
	return list1

post1.py -----------------------> First trial code to achieve multiselect:

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():
    state = SessionState.get(rerun = False, prepro_steps=0)
    name = inp_det('source')
    session_state = SessionState.get(name="", button_sent=False)
    button_sent = st.button("SUBMIT")
    if button_sent:
        session_state.button_sent = True
        if session_state.button_sent:
            listnames = n.show_names(name)
            selectednames=st.multiselect('Select your names',listnames)
            st.write(selectednames)

if __name__ == "__main__":
    main()

post2.py -----------------------> Second trial code to achieve multiselect:

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():
    state = SessionState.get(rerun = False, prepro_steps=0)
    name = inp_det('source')
    session_state = SessionState.get(name="", button_sent=False)
    button_sent = st.button("SUBMIT")
    if button_sent:
        session_state.button_sent = True
        if session_state.button_sent:
            listnames = n.show_names(name)
            for ln in listnames:
                selectednames=st.checkbox(ln, value=False, key=None)
            st.write(selectednames)

if __name__ == "__main__":
    main()

The issue is in both the cases, the multiselect disappears as soon as I select one option. Is there any way for me to select multiple options after some input from user and capture those selected options?

Streamlit: version 0.62.1
Python: version 3.7.4
Browser (Google chrome): Version 83.0.4103.116

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

1 Like

Thank you so much! This works !

1 Like