MrLi008
commited on
Commit
·
4764dbd
1
Parent(s):
76fbbdd
'u'
Browse files- demo-streamlit.py +120 -0
demo-streamlit.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
|
4 |
+
# In-memory "database" for simplicity
|
5 |
+
users_db = {
|
6 |
+
'1': {'password': '1', 'name': '1', 'age': 1, 'favorite_color': 'blue'}
|
7 |
+
}
|
8 |
+
|
9 |
+
def register_user(username, password, name, age):
|
10 |
+
"""Register a new user."""
|
11 |
+
global users_db
|
12 |
+
if username in users_db:
|
13 |
+
return False
|
14 |
+
users_db[username] = {'password': password, 'name': name, 'age': age}
|
15 |
+
return True
|
16 |
+
|
17 |
+
def login_user(username, password):
|
18 |
+
"""Authenticate a user."""
|
19 |
+
if username in users_db and users_db[username]['password'] == password:
|
20 |
+
st.session_state['logged_in'] = True
|
21 |
+
st.session_state['user'] = users_db[username]
|
22 |
+
print('login success')
|
23 |
+
return True
|
24 |
+
print('login faild')
|
25 |
+
print(users_db)
|
26 |
+
return False
|
27 |
+
|
28 |
+
def logout_user():
|
29 |
+
"""Log out the current user."""
|
30 |
+
st.session_state['logged_in'] = False
|
31 |
+
st.session_state.pop('user', None)
|
32 |
+
st.session_state.pop('step1_done', None)
|
33 |
+
st.session_state.pop('step2_done', None)
|
34 |
+
# st.experimental_rerun()
|
35 |
+
|
36 |
+
def show_registration():
|
37 |
+
"""Display the registration form."""
|
38 |
+
st.header("Register")
|
39 |
+
reg_username = st.text_input("Username", key="reg_user")
|
40 |
+
reg_password = st.text_input("Password", type="password", key="reg_pass")
|
41 |
+
reg_name = st.text_input("Name", key="reg_name")
|
42 |
+
reg_age = st.number_input("Age", min_value=0, max_value=120, step=1, key="reg_age")
|
43 |
+
if st.button("Register"):
|
44 |
+
if register_user(reg_username, reg_password, reg_name, reg_age):
|
45 |
+
st.success("Registration successful. Please log in.")
|
46 |
+
else:
|
47 |
+
st.error("Username already exists. Please choose another one.")
|
48 |
+
|
49 |
+
def show_login():
|
50 |
+
"""Display the login form."""
|
51 |
+
st.header("Login")
|
52 |
+
username = st.text_input("Username", key="login_user")
|
53 |
+
password = st.text_input("Password", type="password", key="login_pass")
|
54 |
+
if st.button("Login"):
|
55 |
+
if login_user(username, password):
|
56 |
+
st.success(f"Welcome back, {st.session_state['user']['name']}!")
|
57 |
+
|
58 |
+
else:
|
59 |
+
st.error("Invalid username or password.")
|
60 |
+
|
61 |
+
def show_task():
|
62 |
+
"""Display the multi-step task."""
|
63 |
+
st.title(f"Welcome, {st.session_state['user']['name']}!")
|
64 |
+
|
65 |
+
# Step 1: Confirm personal details
|
66 |
+
st.header("Step 1: Confirm Your Details")
|
67 |
+
name = st.text_input("Name", value=st.session_state['user']['name'])
|
68 |
+
age = st.number_input("Age", value=st.session_state['user']['age'], min_value=0, max_value=120, step=1)
|
69 |
+
if st.button("Submit Step 1"):
|
70 |
+
st.session_state['user']['name'] = name
|
71 |
+
st.session_state['user']['age'] = age
|
72 |
+
st.session_state['step1_done'] = True
|
73 |
+
st.write(f"Hello, {name}. You are {age} years old.")
|
74 |
+
time.sleep(1) # Simulate a processing delay
|
75 |
+
|
76 |
+
# Step 2: Update favorite color
|
77 |
+
if 'step1_done' in st.session_state:
|
78 |
+
st.header("Step 2: Update Your Favorite Color")
|
79 |
+
favorite_color = st.text_input("What's your favorite color?", value=st.session_state['user']['favorite_color'])
|
80 |
+
if st.button("Submit Step 2"):
|
81 |
+
st.session_state['user']['favorite_color'] = favorite_color
|
82 |
+
st.session_state['step2_done'] = True
|
83 |
+
st.write(f"Got it! Your favorite color is {favorite_color}.")
|
84 |
+
time.sleep(1) # Simulate a processing delay
|
85 |
+
|
86 |
+
# Step 3: Final Confirmation and Real-time Processing
|
87 |
+
if 'step2_done' in st.session_state:
|
88 |
+
st.header("Step 3: Final Confirmation")
|
89 |
+
confirm = st.checkbox("Confirm and process the data")
|
90 |
+
if confirm:
|
91 |
+
st.write("Processing data...")
|
92 |
+
progress_bar = st.progress(0)
|
93 |
+
|
94 |
+
# Simulate a real-time processing task
|
95 |
+
for i in range(100):
|
96 |
+
progress_bar.progress(i + 1)
|
97 |
+
time.sleep(0.05) # Simulate real-time data streaming
|
98 |
+
|
99 |
+
st.success("Task Completed!")
|
100 |
+
st.write(f"Summary: {name}, {age} years old, likes {favorite_color}.")
|
101 |
+
|
102 |
+
# Option to logout
|
103 |
+
if st.button("Logout"):
|
104 |
+
logout_user()
|
105 |
+
|
106 |
+
def main():
|
107 |
+
"""Main function to control the app flow."""
|
108 |
+
st.title("Streamlit Application with Login and Multi-step Task")
|
109 |
+
|
110 |
+
if 'logged_in' not in st.session_state:
|
111 |
+
st.session_state['logged_in'] = False
|
112 |
+
|
113 |
+
if not st.session_state['logged_in']:
|
114 |
+
show_registration()
|
115 |
+
show_login()
|
116 |
+
else:
|
117 |
+
show_task()
|
118 |
+
|
119 |
+
if __name__ == "__main__":
|
120 |
+
main()
|