Always Selectbox issue at the first time, but ok after

I have issue with selectbox in st.beta_expander. at the first time, after I select selectbox under st.beta_expander, looks like it does nothing but refreshes the screen, but works normal at the second time and going forward. example codes as below:

def run():
    with st.sidebar:
        st.selectbox('Please choose option', ['A', 'B','C'])
        with st.beta_expander("expand"):
           st.selectbox("Choose 1", ['1','2'])
           st.selectbox("Choose 2", ['3', '4'])

if __name__ == "__main__":
    st.set_option('deprecation.showfileUploaderEncoding', False)
    session_state = SessionState.get(password='')

    if session_state.password != '123':
        pwd_placeholder = st.sidebar.empty()
        pwd = pwd_placeholder.text_input("Password:", value="", type="password")
        session_state.password = pwd
        if session_state.password == '123':
            pwd_placeholder.empty()
            run()
        elif session_state.password != '':
            st.error("the password you entered is incorrect")
    else:
        run()

Hey @calvin,

I would like to give you a hand, but I could use some comments in your code on what you are trying to do, and put some st.write statements in where you think its going wrong.

So far, I have not been able to reproduce your issue with your app!

Happy Streamlit-ing!
Marisa

I just rerun it again. still have the same issue. the following step is what I did:

1, input password, it will go to the next screen.
2, on the second screen, I click ‘+’ to expand.
3, then select option 2 in choose 1 selectbox.
4, after few seconds, it goes back to origin second screen. with expand ‘+’.

please note that it only happens at the first time I run app every time.

Hey @calvin,

Those steps helped me to recreate what you are seeing. Thanks for finding this! So, I have found the same thing is happening on my end. Now, through testing I have found that when this does happen on the first go around, it does actually save your selection of the choose 1 select_box: see my screen shots:
First: this is how it opens if you put in some print statements:


Second: after I have picked ‘2’ in the choose 1 select_box, it does refresh the page, but it has actually saved my selection.

I realize its not ideal, but it shouldn’t stop you from continuing your app! In the meantime, I’m going to send this on to our engineering team and see if they can see whats happening!

Thanks again!
Happy Streamlit-ing!
Marisa

Hey @calvin,

Sorry for the long delay, its been very busy over here! @kmcgrady was able to find some workarounds for your case, here is a trivial example:

import streamlit as st
if st.checkbox("I have something"):
    st.write("Checked")
with st.beta_expander("expand"):
    a = st.selectbox("Choose 1", ['1','2'])

Open the expander and click the checkbox, expander collapses. This is due to the fact that we add/remove something to the tree before expander when the checkbox occurs…expander essentially “rerenders” with its initial state. If we removed the st.write call, everything seems to work as expected.

import streamlit as st
st.checkbox("I have something"):
with st.beta_expander("expand"):
    a = st.selectbox("Choose 1", ['1','2'])

For the time being, the solution is to either avoid rendering the password placeholder when the password is correct or to always render the placeholder so the “structure” of the page doesn’t page. There’s a couple ways to do it.

One uses `experimental_rerun`:
import streamlit as st
import SessionState
def run():
    with st.sidebar:
        st.selectbox('Please choose option', ['A', 'B', 'C'])
        with st.beta_expander("expand"):
            st.selectbox("Choose 1", ['1', '2'])
            st.selectbox("Choose 2", ['3', '4'])
if __name__ == "__main__":
    st.set_option('deprecation.showfileUploaderEncoding', False)
    session_state = SessionState.get(password='')
    if session_state.password == '123':
        run()
    else:
        pwd = st.sidebar.text_input(
            "Password:", value="", type="password")
        if pwd == '123':
            session_state.password = pwd
            st.experimental_rerun()
        elif pwd != '':
            st.error("the password you entered is incorrect")
And one moves the `st.sidebar.empty()` to be rendered in any case:
import streamlit as st
import SessionState
def run():
    with st.sidebar:
        st.selectbox('Please choose option', ['A', 'B', 'C'])
        with st.beta_expander("expand"):
            st.selectbox("Choose 1", ['1', '2'])
            st.selectbox("Choose 2", ['3', '4'])
if __name__ == "__main__":
    st.set_option('deprecation.showfileUploaderEncoding', False)
    session_state = SessionState.get(password='')
    pwd_placeholder = st.sidebar.empty()
    if session_state.password != '123':
        pwd = pwd_placeholder.text_input(
            "Password:", value="", type="password")
        session_state.password = pwd
        if session_state.password == '123':
            pwd_placeholder.empty()
            run()
        elif session_state.password != '':
            st.error("the password you entered is incorrect")
    else:
        run()

Ken has also opened a ticket to address this use case in future!

Hope this helps!
Happy Streamlit-ing!
Marisa

It works for me. Thank you very much!