File size: 4,398 Bytes
4764dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import streamlit as st
import time

# In-memory "database" for simplicity
users_db = {
    '1': {'password': '1', 'name': '1', 'age': 1, 'favorite_color': 'blue'}
}

def register_user(username, password, name, age):
    """Register a new user."""
    global users_db
    if username in users_db:
        return False
    users_db[username] = {'password': password, 'name': name, 'age': age}
    return True

def login_user(username, password):
    """Authenticate a user."""
    if username in users_db and users_db[username]['password'] == password:
        st.session_state['logged_in'] = True
        st.session_state['user'] = users_db[username]
        print('login success')
        return True
    print('login faild')
    print(users_db)
    return False

def logout_user():
    """Log out the current user."""
    st.session_state['logged_in'] = False
    st.session_state.pop('user', None)
    st.session_state.pop('step1_done', None)
    st.session_state.pop('step2_done', None)
    # st.experimental_rerun()

def show_registration():
    """Display the registration form."""
    st.header("Register")
    reg_username = st.text_input("Username", key="reg_user")
    reg_password = st.text_input("Password", type="password", key="reg_pass")
    reg_name = st.text_input("Name", key="reg_name")
    reg_age = st.number_input("Age", min_value=0, max_value=120, step=1, key="reg_age")
    if st.button("Register"):
        if register_user(reg_username, reg_password, reg_name, reg_age):
            st.success("Registration successful. Please log in.")
        else:
            st.error("Username already exists. Please choose another one.")

def show_login():
    """Display the login form."""
    st.header("Login")
    username = st.text_input("Username", key="login_user")
    password = st.text_input("Password", type="password", key="login_pass")
    if st.button("Login"):
        if login_user(username, password):
            st.success(f"Welcome back, {st.session_state['user']['name']}!")

        else:
            st.error("Invalid username or password.")

def show_task():
    """Display the multi-step task."""
    st.title(f"Welcome, {st.session_state['user']['name']}!")

    # Step 1: Confirm personal details
    st.header("Step 1: Confirm Your Details")
    name = st.text_input("Name", value=st.session_state['user']['name'])
    age = st.number_input("Age", value=st.session_state['user']['age'], min_value=0, max_value=120, step=1)
    if st.button("Submit Step 1"):
        st.session_state['user']['name'] = name
        st.session_state['user']['age'] = age
        st.session_state['step1_done'] = True
        st.write(f"Hello, {name}. You are {age} years old.")
        time.sleep(1)  # Simulate a processing delay

    # Step 2: Update favorite color
    if 'step1_done' in st.session_state:
        st.header("Step 2: Update Your Favorite Color")
        favorite_color = st.text_input("What's your favorite color?", value=st.session_state['user']['favorite_color'])
        if st.button("Submit Step 2"):
            st.session_state['user']['favorite_color'] = favorite_color
            st.session_state['step2_done'] = True
            st.write(f"Got it! Your favorite color is {favorite_color}.")
            time.sleep(1)  # Simulate a processing delay

    # Step 3: Final Confirmation and Real-time Processing
    if 'step2_done' in st.session_state:
        st.header("Step 3: Final Confirmation")
        confirm = st.checkbox("Confirm and process the data")
        if confirm:
            st.write("Processing data...")
            progress_bar = st.progress(0)

            # Simulate a real-time processing task
            for i in range(100):
                progress_bar.progress(i + 1)
                time.sleep(0.05)  # Simulate real-time data streaming

            st.success("Task Completed!")
            st.write(f"Summary: {name}, {age} years old, likes {favorite_color}.")

    # Option to logout
    if st.button("Logout"):
        logout_user()

def main():
    """Main function to control the app flow."""
    st.title("Streamlit Application with Login and Multi-step Task")

    if 'logged_in' not in st.session_state:
        st.session_state['logged_in'] = False

    if not st.session_state['logged_in']:
        show_registration()
        show_login()
    else:
        show_task()

if __name__ == "__main__":
    main()