Summary
I’ve added pagination in my app, and am using st.form() and st.form_submit_button() to pass data to the st.session_state() and navigate to the next page. I’ve noticed that using st.selectbox() will successfully pass the input information to the next page, but st.multiselect() will not, and neither does st.text_input(). My end state will allow the user to select multiple options, hit “Submit” in the form, and then navigate to the next page, and on the next page, the selected options from the st.multiselect will be available.
Steps to reproduce
Code snippet:
import streamlit as st
if 'current_page_num' not in st.session_state:
st.session_state['current_page_num'] = 0
def process_selected_selected_colleagues(selected_input_option_selected_colleagues):
st.session_state['selected_colleagues'] = selected_input_option_selected_colleagues
def process_selected_second_form(selected_input_option_second_form):
st.write(selected_input_option_second_form)
def click_next(selected_input_option_selected_colleagues=None, selected_input_option_second_form=None):
max_page = 3
if st.session_state['current_page_num'] + 1 > max_page:
st.session_state['current_page_num'] = 0
else:
if selected_input_option_selected_colleagues is not None:
process_selected_selected_colleagues(selected_input_option_selected_colleagues)
elif selected_input_option_second_form is not None:
process_selected_second_form(selected_input_option_second_form)
st.session_state['current_page_num'] += 1
def click_prev():
max_page = 3
if st.session_state['current_page_num'] - 1 < 0:
st.session_state['current_page_num'] = max_page
else:
st.session_state['current_page_num'] -= 1
def page0():
st.session_state['current_page_num'] = 0
if 'input_name' in st.session_state:
submit_colleagues = st.form('submit_colleages')
with submit_colleagues:
submit_colleagues.write('The following people work in your workplace:')
all_colleages = ['Adam', 'Betsy', 'Colleen', 'David', 'Edward', 'Fiona', 'Gary', 'Hasan', 'Isabelle', 'Jeremiah', 'Kazuki']
submit_colleagues.write(all_colleages)
# st.selectbox works as expected
select_colleagues = st.selectbox('Select Colleagues', options=all_colleages)
# HELP - st.multiselect(), st.text_input() neither of these pass values to the next page
#select_colleagues = st.multiselect('Select Colleagues', options=all_colleages)
colleages_submitted = submit_colleagues.form_submit_button(
'Submit',
on_click=click_next,
args=(select_colleagues, None)
)
def page1():
st.session_state['current_page_num'] = 1
st.write(st.session_state['selected_colleagues'])
sidebar_form = st.sidebar.form('sidebar_form')
with sidebar_form:
input_name = sidebar_form.text_input('What is your name?')
input_name_submitted = sidebar_form.form_submit_button('Submit Name')
if input_name_submitted:
st.session_state['input_name'] = input_name
display_next = True if len(input_name) > 0 and 'selected_colleagues' in st.session_state else False
display_prev = True if st.session_state['current_page_num'] > 1 else False
_, prev, next, _ = st.columns([5, 0.8, 0.8, 5])
if display_next and input_name_submitted:
next.button(label='Next', on_click=click_next)
elif not display_next and input_name_submitted:
next.button(label='Next', on_click=click_next, disabled=True)
if display_prev and input_name_submitted:
prev.button(label='Prev', on_click=click_prev)
elif not display_prev and input_name_submitted:
prev.button(label='Prev', on_click=click_prev, disabled=True)
pages = {
0: page0,
1: page1
}
pages[st.session_state['current_page_num']]()
You can run this script as is, it should work given the same versions of python / streamlit. As you can see in the page0() function, I have both an st.selectbox() and an st.multiselect(). If you comment out the st.selectbox() line, and instead use the st.multiselect(), you’ll see the misconstrued behavior explained below:
Expected behavior:
After the user selects a number of colleagues via the st.multiselect(), the list of names should be saved into st.session_state[‘selected_colleagues’] , similar to how this happens with the st.selectbox() , and should be printed with page1() runs.
Actual behavior:
When using the st.multiselect(), all values selected by the user are not saved into the st.session_state key ‘selected_colleagues’.
Debug info
- Streamlit version: 1.20.0
- Python version: 3.8
- Using Conda
- OS version: Windows 10 Enterprise Build 19042.2604
- Browser version: Microsoft Edge Version 111.0.1661.62 (Official build) (64-bit)