Multiselect reinitializing the page

Hello community,
I have encountered several times now this behavior but this time with specifically with the multiselect object.

Scenario:
I have a button, once clicked it will show me a text and a multiselect object.

Weird behavior:
Once I select the options I want the page reinitialize without printing anything.

Here is two methods I tried, they gave me the same behavior

Method 1:

import streamlit as st


btn= st.button ('Start')
dates = ['2021-01-01',
                            '2021-01-02',
                            '2021-01-03',
                            '2021-01-04',
                            ]
if btn :
    
    st.write('123')
    
    output = st.multiselect('Test', options = dates)
    if output != []:
        st.write(output)
    else:
        st.write('haha')

Method 2

import streamlit as st
btn= st.button ('Start')
dates = ['2021-01-01',
                            '2021-01-02',
                            '2021-01-03',
                            '2021-01-04',
                            ]
if btn :
    
    st.write('123')
  
    #option 2 
    with st.form(key='rand'):
        selected_dates = st.multiselect('Test 123', options = dates)
        submit_button = st.form_submit_button(label='Validate')
    if submit_button:
        stcol_load.write(selected_dates)

Any idea what I am doing wrong? Ideas are appreciated.
Thank you,

Hi @Rima ,

If I understood you correctly, you would like to get the selected date as output, but instead your app returns to the initial state, right ?

This is because of Streamlit’s behaviour of running the app linearly from the very top. Hence, it gets stuck at st.button syntax, and you see no output. A quick workaround, (not ideal) using st.checkbox option. So instead of

Use, check_state = st.checkbox("Start")

One of other possibilities, is to use st.session_state and storing the button’s state.

I hope this helps.
Best,
Avra

Hello Avra,
Thank you for getting back,
Indeed, I tried to implement the st.session_state method, even though I encountered a related problem.

Here is the modified code:

Import importlib

import streamlit as st
import time 
st.session_state.button_sent = None
dates = ['2021-01-01',
                            '2021-01-02',
                            '2021-01-03',
                            '2021-01-04',
                            ]


btn= st.checkbox ('Start')
placeholder_bar = st.empty()
placeholder_bar.progress(0)
placeholder_bar.progress(30)
time.sleep(5)
placeholder_bar.progress(50)
st.write(st.session_state.button_sent)
if btn or st.session_state.button_sent:
    st.session_state.button_sent = True
    st.write('123')   
    ##option 1
    output = st.multiselect('Test', options = dates)
    st.write(output)

the print after the select works well, but the problem is that whenever I select now the options from the select box the whole page refreshed and the first couple of lines (before the select box) re run.

Sure, here I put simple examples with progress bar (0, 30, 50) but in my case the code prior to multiselect button takes more time and I find it weird to re run it on every multiselect selections.

Hi @Rima ,

So, the possible workaround is , if you wrap around those lines of code prior to multi select as a single function, and use @st.experimental_memo . Please do refer to the doc.

So, in your given example,

@st.experimental_memo(suppress_st_warning = True)
def pbar():
    placeholder_bar = st.empty()
    placeholder_bar.progress(0)
    placeholder_bar.progress(30)
    time.sleep(5)
    placeholder_bar.progress(50)
    

I won’t say this is the best code practice, but yes it does the task which you need,

I hope this works out.
Best,
Avra

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