Project Brief
Today’s task is to create a signup-login web interface with Streamlit.
Expected Output
Watch the following video to see what the output of the app that I built.
You can store the username and passwords in a JSON file.
👉 Please consider storing the passwords as hashed passwords so that they are not visible in the JSON file for security reasons.
Environment Setup Instructions
Install the required libraries with:
pip install streamlit
Execute with streamlit run main.py and open
http://localhost:8501 in your browser to see the webpage.
Project Solution
Feel free to post your questions or comments in the comment section below.
Happy Coding!
Daily Python Projects Team
Hi,
I just completed the task.
Below is my code:
import streamlit as st
# Initialize session state for user_data if not already present
if 'user_data' not in st.session_state:
st.session_state['user_data'] = {}
def home():
st.title("Simple Login App")
st.header("Home")
st.write("Welcome to Simple Login App. Please use the menu to log in or sign up.")
def signup():
st.title("Simple Login App")
st.header("Create a New Account")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
# confirm_password = st.text_input("Confirm Password", type="password")
if st.button("Sign Up"):
if username in st.session_state['user_data']:
st.error("Username already exists. Please choose a different username.")
# elif password != confirm_password:
# st.error("Passwords do not match.")
else:
st.session_state['user_data'][username] = password
st.success("You have successfully signed up!")
st.info("Please go to the Login page to log in.")
def login():
st.title("Simple Login App")
st.header("Login Section")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
if username not in st.session_state['user_data']:
st.error("Username does not exist. Please sign up first.")
elif st.session_state['user_data'][username] != password:
st.error("Incorrect password. Please try again.")
else:
st.success(f"Welcome {username.title()}!")
st.write("You have successfully logged in.")
st.header("Inner Page")
st.write("This is a protected inner page that only logged in user can see.")
def main():
# st.sidebar.header("Menu")
choice = (st.sidebar.selectbox("Menu", ["Home", "Login", "Sign Up"]))
if choice == "Sign Up":
signup()
elif choice == "Login":
login()
elif choice == "Home":
home()
if __name__ == "__main__":
main()