Sidebar gets displayed all the time despite check

I am new to using Streamlit and am creating an app which must have a login page and after login, it must display some query results. The results page must have a sidebar. However, the sidebar shows up now even on the login page despite the sidebar code being inside the function that renders the result page only after a condition checks True. The code is roughly as follows. What am I missing or doing wrong?

def app_title():
    st.title("App Title")
    st.write("App Description")
    
def login_page(placeholder):
    with placeholder:
        login = st.container()
        with login:
            app_title()
            st.write("\n")
            st.write("\n")
            st.subheader("Login")
            st.text_input("Username")
            pwd = st.text_input("Password",type="password")
            if len(pwd) > 0:
                return True
    return False

def results_page(placeholder):
    with placeholder:
        results = st.container()
        with results:
            app_title()
            st.write("\n")
            st.write("\n")
            sps_list = ['XYZ', 'ABC']
            selected_sps = st.sidebar.selectbox("Choose sps", sps_list)
            from_date = st.sidebar.date_input('From', date.today())
            to_date = st.sidebar.date_input('To', date.today())
            submit = st.sidebar.button("Submit")
                    
            if submit:
                st.markdown(f"\n**Results for {selected_sps}**")
                res = search_for_sps(selected_sps, from_date, to_date)
                metadata = fetch_metadata(res)
                metadata_df = pd.DataFrame(metadata)
                st.dataframe(metadata_df)
                
def main():
    main_placeholder = st.empty()
    success = login_page(main_placeholder)
    if success:
        main_placeholder.empty()
        results_page(main_placeholder)
        
if __name__ == "__main__":
    main()

Hi, if you do not want to let the sidebar expanded, you can set like this:

import streamlit as st
st.set_page_config(initial_sidebar_state="collapsed")

[your other code put here]

Thank you!

I tried it out and It now collapses the sidebar but somehow the placeholder isn’t getting cleared after entering login credentials. Also, I didn’t understand the execution flow - how the sidebar was getting displayed in the first place despite it being inside a function that must execute only when the condition turns True (which is the case only after entering the password).

you can expand and let sidebar show at any part of your code, whatever in the start, middle or end.

1 Like

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