# Display the selected page content
if selected_page == "Home":
home_page.show()
elif selected_page == "Profile":
profile_page.show()
Hi, Recently i started learning streamlit, i have simple login screen. I have few queries how do i achieve it. Like my initial logion should only be login page. after successful login user should have access of the rest of the pages.
Here is an example. Create a session variable in main.py and page1.py.
As session variable is like a global variable. But it gets wiped out when you refresh the page. Hence it only exists per session basis.
Search and study session state. This is very important in manipulating variables around streamlit app.
Folder structure
main.py
pages/
page1
Codes
main.py
if 'oklogin' not in st.session_state:
# This is our variable.
st.session_state.oklogin = False
if email == actual_email and password == actual_password:
st.session_state.oklogin = True
if st.session_state.oklogin:
# Your content here
page1.py
if 'oklogin' not in st.session_state:
st.session_state.oklogin = False
if st.session_state.oklogin:
# Your content here
else:
st.error('Please log in in the main page.')
import streamlit as st
from pages import page1
# Page for login
if not hasattr(st.session_state, 'username') or st.session_state.username is None:
st.header('Login')
# Create an empty container
placeholder = st.empty()
actual_email = "email"
actual_password = "password"
# Insert a form in the container
with placeholder.form("login"):
st.markdown("#### Enter your credentials")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
submit = st.form_submit_button("Login")
if 'oklogin' not in st.session_state:
# This is our variable.
st.session_state.oklogin = False
if email == actual_email and password == actual_password:
st.session_state.oklogin = True
if st.session_state.oklogin:
# Your content here
if email == actual_email and password == actual_password:
placeholder.empty()
st.success("Login successful")
st.session_state.username = email
st.session_state.page = page1
else:
st.error("Invalid credentials. Please try again.")
page1.py
import streamlit as st
if 'oklogin' not in st.session_state:
st.session_state.oklogin = False
if st.session_state.oklogin:
st.info('Home Page')
st.balloons()
else:
st.error('Please log in in the main page.')
still in initial login page 1 is visible, with info “Please log in in the main page”, it should be hidden or not available until the sucessfull login. and post sucessfull login main.py (login.py) should not be visible, only the list of pages should be available.
Under normal streamlit usage this is not possible. But instead of totally hiding the page why not just make it empty or not showing its contents.
So we can have for example a home page, login page and profile page. At the start the main page will be rendered since we have not logged in yet we will not show its content. Then we switch to login page to login. Once logged in, we will not show its contents and switch to main page and this time show its content. In summary if we are not logged in yet, all pages’s content will not be displayed except the login page. Once logged in, all pages’s content will be visible except the login page. The logout button will be shown in the main page.
Minimal example code
main.py
import streamlit as st
USERS = {
"info": [
{
"username": "johnwall",
"email": "john@gmail.com",
"password": "Kqhml5dw"
},
{
"username": "peterwall",
"email": "peter@gmail.com",
"password": "t4Pu78bV"
}
]
}
if 'profile' not in st.session_state:
st.session_state.profile = {}
if 'oklogin' not in st.session_state:
st.session_state.oklogin = False
st.switch_page('pages/login.py')
def logout_cb():
st.session_state.oklogin = False
# Show content if logged in.
if st.session_state.oklogin:
st.subheader('Main page')
with st.sidebar:
st.button('Logout', on_click=logout_cb)
st.write('List of Users')
st.dataframe(USERS['info'])
pages/login.py
import time
import streamlit as st
from main import USERS
if 'oklogin' not in st.session_state:
st.session_state.oklogin = False
if 'profile' not in st.session_state:
st.session_state.profile = {}
def login_cb():
"""Updates oklogin state if form submit is clicked."""
for ui in USERS['info']:
if (ui['email'] == st.session_state.email
and ui['password'] == st.session_state.password):
st.session_state.oklogin = True
st.session_state.profile = ui
break
if not st.session_state.oklogin:
st.error('email and/or password are incorrect.')
# Show login form.
if not st.session_state.oklogin:
with st.form('login form', clear_on_submit=True):
st.text_input('Email', key='email')
st.text_input('Password', key='password', type='password')
st.form_submit_button('Login', on_click=login_cb)
else:
time.sleep(0.1) # make the page switch visible
st.switch_page('main.py')
pages/profile.py
import streamlit as st
if 'profile' not in st.session_state:
st.session_state.profile = {}
if 'oklogin' not in st.session_state:
st.session_state.oklogin = False
st.switch_page('pages/login.py')
# Show content if logged in.
if st.session_state.oklogin:
st.subheader('Profile')
st.write("User's profile")
st.json(st.session_state.profile)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.