Create a Streamlit application which required a Username & Password to enter. The application should display something interesting (it's a password demo)
To upload files, please first save the app
import streamlit as st
import time
# Initialize session state variables
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "failed_attempts" not in st.session_state:
st.session_state.failed_attempts = 0
# Credentials (in a real app, these should be stored securely)
CORRECT_USERNAME = "demo"
CORRECT_PASSWORD = "password123"
def authenticate(username, password):
if username == CORRECT_USERNAME and password == CORRECT_PASSWORD:
st.session_state.authenticated = True
st.session_state.failed_attempts = 0
st.toast("Login successful! 🎉", icon="✅")
st.balloons()
else:
st.session_state.failed_attempts += 1
st.error(f"Invalid credentials! Attempt {st.session_state.failed_attempts}")
if st.session_state.failed_attempts >= 3:
st.warning("Too many failed attempts. Please wait 5 seconds...")
time.sleep(5)
st.session_state.failed_attempts = 0
# Main app layout
st.title("🔐 Secure Application Demo")
if not st.session_state.authenticated:
with st.form("login_form"):
st.subheader("Please Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submit = st.form_submit_button("Login")
if submit:
authenticate(username, password)
# Show hint
with st.expander("Need a hint?"):
st.write("Username: demo")
st.write("Password: password123")
else:
# The protected content
st.success("Welcome to the secure area! 🚀")
st.header("Random Fun Facts Dashboard")
# Create some interesting content
col1, col2 = st.columns(2)
with col1:
st.metric("Daily Active Users", "1,234", "+12%")
st.bar_chart({"Data": [1, 5, 2, 6, 2, 1]})
with col2:
st.metric("Customer Satisfaction", "98%", "+2%")
st.line_chart({"Trends": [3, 1, 4, 1, 5, 9]})
# Add some interactive elements
if st.button("Generate Snow Effect"):
st.snow()
# Logout button
if st.button("Logout"):
st.session_state.authenticated = False
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?