artintel235 commited on
Commit
0d7b164
·
verified ·
1 Parent(s): f2d7821

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -17
app.py CHANGED
@@ -1,30 +1,87 @@
1
  import streamlit as st
2
 
3
- # Initialize session state
 
 
4
  if "logged_in" not in st.session_state:
5
  st.session_state.logged_in = False
 
 
6
 
7
- # Callback for login
8
- def login_callback():
9
- username = st.session_state.username_input
10
- password = st.session_state.password_input
11
- if username == "admin" and password == "password": # Replace with your credentials
 
 
 
 
 
 
12
  st.session_state.logged_in = True
 
 
13
  else:
14
- st.error("Invalid username or password")
15
 
16
- # Callback for logout
17
- def logout_callback():
18
  st.session_state.logged_in = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Login form
21
- if not st.session_state.logged_in:
22
- st.subheader("Login")
23
- st.text_input("Username", key="username_input")
24
- st.text_input("Password", type="password", key="password_input")
25
- st.button("Login", on_click=login_callback)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Logout button
28
  if st.session_state.logged_in:
29
- st.subheader("Welcome to the App!")
30
- st.button("Logout", on_click=logout_callback)
 
 
 
1
  import streamlit as st
2
 
3
+ # Initialize session state for user data and authentication
4
+ if "users" not in st.session_state:
5
+ st.session_state.users = {} # Store registered users (username: password)
6
  if "logged_in" not in st.session_state:
7
  st.session_state.logged_in = False
8
+ if "current_user" not in st.session_state:
9
+ st.session_state.current_user = None
10
 
11
+ # Registration function
12
+ def register(username, password):
13
+ if username in st.session_state.users:
14
+ st.error("Username already exists. Please choose a different username.")
15
+ else:
16
+ st.session_state.users[username] = password
17
+ st.success("Registration successful! Please log in.")
18
+
19
+ # Login function
20
+ def login(username, password):
21
+ if username in st.session_state.users and st.session_state.users[username] == password:
22
  st.session_state.logged_in = True
23
+ st.session_state.current_user = username
24
+ st.success("Logged in successfully!")
25
  else:
26
+ st.error("Invalid username or password.")
27
 
28
+ # Logout function
29
+ def logout():
30
  st.session_state.logged_in = False
31
+ st.session_state.current_user = None
32
+ st.info("Logged out successfully!")
33
+
34
+ # Registration form
35
+ def registration_form():
36
+ with st.form("Registration"):
37
+ st.subheader("Register")
38
+ username = st.text_input("Username (min 5 characters)", key="reg_username")
39
+ password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
40
+ submit_button = st.form_submit_button("Register")
41
+
42
+ if submit_button:
43
+ if len(username) < 5:
44
+ st.error("Username must be at least 5 characters long.")
45
+ elif len(password) < 6:
46
+ st.error("Password must be at least 6 characters long.")
47
+ else:
48
+ register(username, password)
49
 
50
  # Login form
51
+ def login_form():
52
+ with st.form("Login"):
53
+ st.subheader("Login")
54
+ username = st.text_input("Username", key="login_username")
55
+ password = st.text_input("Password", type="password", key="login_password")
56
+ submit_button = st.form_submit_button("Login")
57
+
58
+ if submit_button:
59
+ if len(username) < 5:
60
+ st.error("Username must be at least 5 characters long.")
61
+ elif len(password) < 6:
62
+ st.error("Password must be at least 6 characters long.")
63
+ else:
64
+ login(username, password)
65
+
66
+ # Main app screen (after login)
67
+ def main_app():
68
+ st.subheader(f"Welcome, {st.session_state.current_user}!")
69
+ st.write("Enter a name below to get a greeting.")
70
+
71
+ # Input field for name
72
+ name = st.text_input("Enter a name", key="name_input")
73
+
74
+ # Display greeting in real-time
75
+ if name:
76
+ st.write(f"Hi {name}!")
77
+
78
+ # Logout button
79
+ if st.button("Logout"):
80
+ logout()
81
 
82
+ # Render the appropriate screen based on login status
83
  if st.session_state.logged_in:
84
+ main_app()
85
+ else:
86
+ registration_form()
87
+ login_form()