Streamlit webapp with sidebar refreshes everytime when some action is performed

My code:

import streamlit as st
import pandas as pd

def main():
    st.markdown("<h1 style='text-align: center; color: white;'>My Streamlit App</h1>", unsafe_allow_html=True)

    menu = ["Admin","Manager","Employee"]
    choice = st.sidebar.selectbox("User",menu)

    if choice == "Admin":
        admid = st.sidebar.text_input("Admin ID")
        passw = st.sidebar.text_input("Password",type='password')

        if st.sidebar.button("Login"):
            result = 1
            if result:
                user = st.selectbox("Select User",["Admin","Manager","Employee"])
                if user == "Admin":
                    task = st.selectbox("Manage Admin",["View","Add","Delete"])
                    if task == "View":
                        st.subheader("Admins")
                    if task == "Add":
                        st.subheader("Add admin")
                    if task == "Delete":
                        st.subheader("Delete admin")
                elif user == "Manager":
                    task = st.selectbox("Manage Manager",["View","Add","Delete"])
                    if task == "View":
                        st.subheader("Managers")
                    if task == "Add":
                        st.subheader("Add manager")
                    if task == "Delete":
                        st.subheader("Delete manager")
                else:
                    task = st.selectbox("Manage Employee",["View","Add","Delete"])
                    if task == "View":
                        st.subheader("Employees")
                    if task == "Add":
                        st.subheader("Add employee")
                    if task == "Delete":
                        st.subheader("Delete employee")
            else:
                st.warning("Incorrect Admin ID/Password")

    elif choice == "Manager":
        manid = st.sidebar.text_input("Manager ID")
        passw = st.sidebar.text_input("Password",type='password')
        if st.sidebar.button("Login"):
            result = 1
            if result:
                st.success("Logged In as Admin")
            else:
                st.warning("Incorrect Manager ID/Password")

    else:
        empid = st.sidebar.text_input("Employee ID")
        passw = st.sidebar.text_input("Password",type='password')
        if st.sidebar.button("Login"):
            result = 1
            if result:
                st.success("Logged In as Admin")
            else:
                st.warning("Incorrect Employee ID/Password")

main()

After I run this app and login as admin / manager / employee using sidebar , doing any further action ( using any selectbox ) refreshes the entire app, making the whole app useless. I’ve read many posts that suggest using session state to overcome this problem. But I couldn’t understand how to implement session state. I’ve read about it and watched YouTube videos, still I’m not able to implement it on my own problem. So if anyone could implement any technique to retain all actions for each run on this code, that’d be easy for me to understand session state.

I’ve gone through many Streamlit posts that discuss the same problem, But I couldn’t understand them.

Hello @MainamarbuSav, welcome to the forum!

This is due to st.button not retaining any state. Like other posts on this matter, your example assume that st.button toggles some state automatically, like a checkbox would do. In practice, st.button returns True for one run only.

Instead of displaying your user views if the login button is clicked, display them if some session state tells you that your user is logged in. Your login button will be used to change that session state.

import streamlit as st
import pandas as pd

def main():
    st.markdown("<h1 style='text-align: center; color: white;'>My Streamlit App</h1>", unsafe_allow_html=True)

    # Initializes session state
    if "user" not in st.session_state:
        # Will store the currently logged user
        st.session_state.user = None

    # Case where user is not logged
    menu = ["Admin","Manager","Employee"]
    choice = st.sidebar.selectbox("User",menu)

    if choice == "Admin":
        admid = st.sidebar.text_input("Admin ID")
        passw = st.sidebar.text_input("Password",type='password')

        if st.sidebar.button("Login"):
            result = 1
            if result:
                st.success("Logged In as Admin")
                st.session_state.user = "Admin"
            else:
                st.warning("Incorrect Admin ID/Password")

    elif choice == "Manager":
        manid = st.sidebar.text_input("Manager ID")
        passw = st.sidebar.text_input("Password",type='password')

        if st.sidebar.button("Login"):
            result = 1
            if result:
                st.success("Logged In as Manager")
                st.session_state.user = "Manager"
            else:
                st.warning("Incorrect Manager ID/Password")

    else:
        empid = st.sidebar.text_input("Employee ID")
        passw = st.sidebar.text_input("Password",type='password')

        if st.sidebar.button("Login"):
            result = 1
            if result:
                st.success("Logged In as Employee")
                st.session_state.user = "Employee"
            else:
                st.warning("Incorrect Employee ID/Password")

    # Case where Admin is logged
    if st.session_state.user == "Admin":
        user = st.selectbox("Select User",["Admin","Manager","Employee"])

        if user == "Admin":
            task = st.selectbox("Manage Admin",["View","Add","Delete"])
            if task == "View":
                st.subheader("Admins")
            if task == "Add":
                st.subheader("Add admin")
            if task == "Delete":
                st.subheader("Delete admin")
        elif user == "Manager":
            task = st.selectbox("Manage Manager",["View","Add","Delete"])
            if task == "View":
                st.subheader("Managers")
            if task == "Add":
                st.subheader("Add manager")
            if task == "Delete":
                st.subheader("Delete manager")
        else:
            task = st.selectbox("Manage Employee",["View","Add","Delete"])
            if task == "View":
                st.subheader("Employees")
            if task == "Add":
                st.subheader("Add employee")
            if task == "Delete":
                st.subheader("Delete employee")

    elif st.session_state.user == "Manager":
        st.write("Manager dashboard")

    elif st.session_state.user == "Employee":
        st.write("Employee dashboard")

main()
2 Likes

Thank you so much for explaining with code

1 Like

Am having this same problem but i dont get this explaination

1 Like

Please share your code here. I’ll see if I could do something, or someone else will help.