File size: 2,545 Bytes
f630872
b072499
 
f630872
b072499
 
 
 
 
 
b43b93a
 
b072499
b43b93a
 
b072499
 
 
 
f630872
 
b072499
f630872
b072499
 
 
b43b93a
b072499
b43b93a
 
b072499
 
 
f630872
 
350c15f
 
 
 
 
613e418
350c15f
f630872
b43b93a
 
b072499
 
b43b93a
f630872
b072499
f630872
 
b43b93a
 
 
 
 
 
 
 
b072499
b43b93a
 
 
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
import streamlit as st
import os
import json

def save_user_data(email, data):
    """Save user data to a file named after the user's email."""
    with open(f"{email}.txt", "w") as file:
        file.write(json.dumps(data))

def load_user_data(email):
    """Load user data from a file named after the user's email, or create a new file if it doesn't exist."""
    if not os.path.isfile(f"{email}.txt"):
        return {'email': '', 'phone': '', 'password': ''}
    with open(f"{email}.txt", "r") as file:
        return json.loads(file.read())

def list_saved_users():
    """List all files (users) with saved states."""
    return [f[:-4] for f in os.listdir() if f.endswith('.txt')]

def main():
    st.title('User Data Management')

    # Sidebar for selecting a user file
    st.sidebar.title("Saved Users")
    saved_users = list_saved_users()
    selected_user = st.sidebar.selectbox("Select a user", options=['New User'] + saved_users)

    # Load or initialize data
    if selected_user and selected_user != 'New User':
        cached_data = load_user_data(selected_user)
    else:
        cached_data = {'email': '', 'phone': '', 'password': ''}

    # Input fields with emojis
    new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
    new_phone = st.text_input("πŸ“± Mobile Phone", value=cached_data['phone'])
    show_password = st.checkbox("Show password")
    if show_password:
        new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'])
    else:
        new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'], type='password')

    # Save data when changes are made
    if new_email and (new_email != cached_data['email'] or new_phone != cached_data['phone'] or new_password != cached_data['password']):
        cached_data = {'email': new_email, 'phone': new_phone, 'password': new_password}
        save_user_data(new_email, cached_data)
        st.sidebar.success("Data updated and saved!")

    st.write("Current Data:")
    st.json(cached_data)

    # Password Reset Simulation
    if st.sidebar.button("Reset Password"):
        reset_email = st.sidebar.text_input("Enter email for password reset")
        if reset_email and reset_email in saved_users:
            reset_data = load_user_data(reset_email)
            reset_data['password'] = 'new_password'  # Reset password
            save_user_data(reset_email, reset_data)
            st.sidebar.success(f"Password reset for {reset_email}")

# Run the app
if __name__ == "__main__":
    main()