Example : A Streamlit program to create a Login Page with Static Validation Value.
import streamlit as st
# Dummy user credentials
users = {
"admin": "admin123",
"abc": "abc123",
"xyz": "xyz123"
}
# Page configuration
st.set_page_config(page_title="Login Page", layout="centered")
# Center the content using columns
col1, col2, col3 = st.columns([1, 2, 1]) # center column is wider
with col2: # place everything in the center column
#st.image("https://cdn-icons-png.flaticon.com/512/3064/3064197.png", width=80)
st.title("π User Login")
st.subheader("Login to Your Account")
with st.form("login_form"):
username = st.text_input("Username", placeholder="Enter your username")
password = st.text_input("Password", placeholder="Enter your password", type="password")
col11, col12 = st.columns([1, 3])
login1 = col11.form_submit_button("Login")
reset1 = col12.form_submit_button("Reset")
if login1:
if not username or not password:
st.warning("β οΈ Please fill in both username and password.")
elif username in users and users[username] == password:
st.success(f"β
Welcome User, {username}!")
st.balloons()
else:
st.error("β Invalid username or password.")
NB:
(i) The 'unsafe_allow_html=True' tells Streamlit that "Yes, I trust this HTML and want it to be rendered as-is" used in the code.
(ii) Here, "username in users and users[username] == password:" line is a part of a login check using a dictionary in Python where 'username in users:' is the username entered by the user a key in the users dictionary and 'users[username] == password:' says that the stored password for that username match the password the user typed or not.
Example : A Streamlit program to create a Login Page with Dynamic Validation Value from a MySQL database.
your_project/
β
βββ main.py β Login page (default starting point)
βββ pages/
β βββ dashboard.py β Dashboard page (opened after login)
----------------------- OR --------------------
main.py (Login Page)
import streamlit as st
from sqlalchemy import create_engine, text
# --- MySQL Connection ---
engine = create_engine("mysql+pymysql://root:@localhost/ejs_forms_db")
# --- Check credentials ---
def validate_login(username, password):
with engine.connect() as conn:
result = conn.execute(
text("SELECT * FROM users WHERE username = :u AND password = :p"),
{"u": username, "p": password}
)
return result.fetchone() is not None
# --- Session state ---
if "logged_in" not in st.session_state:
st.session_state.logged_in = False
# --- Login Form ---
def login_form():
st.title("π Login Page")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
login = st.button("Login")
if login:
if validate_login(username, password):
st.session_state.logged_in = True
st.session_state.username = username # optional for dashboard
st.success("β
Login successful")
st.switch_page("pages/dashboard.py") # <-- this will redirect!
else:
st.error("β Invalid username or password")
# --- Run login ---
login_form()
NB: Here, 'logged_in' is a Boolean flag stored in st.session_state to track whether the user is currently logged in.
This line ensures that the logged_in session state variable is initialized i.e. -
- If logged_in = False: the user hasn't logged in yet.
- If logged_in = True: the user has successfully logged in.
This boolean variable is useful for:-
- Controlling access to specific parts of the app and,
- Remembering the user is logged in even if they interact with other pages or rerun the app.
-------------------- OR ---------------------
dashboard.py (pages/dashboard.py after successfull login)
import streamlit as st
# --- Access Control ---
if "logged_in" not in st.session_state or not st.session_state.logged_in:
st.error("β Access denied. Please login first.")
st.stop()
# --- Dashboard UI ---
st.title("π Dashboard")
st.success(f"Welcome the logged in user = {st.session_state.get('username', 'User')}!")
if st.button("Logout"):
st.session_state.logged_in = False
st.switch_page("main.py") # Redirect to login
0 Comments