awacke1 commited on
Commit
b43b93a
Β·
verified Β·
1 Parent(s): 3a7449b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -23
app.py CHANGED
@@ -8,12 +8,11 @@ def save_user_data(email, data):
8
  file.write(json.dumps(data))
9
 
10
  def load_user_data(email):
11
- """Load user data from a file named after the user's email."""
12
- try:
13
- with open(f"{email}.txt", "r") as file:
14
- return json.loads(file.read())
15
- except FileNotFoundError:
16
  return {'email': '', 'phone': '', 'password': ''}
 
 
17
 
18
  def list_saved_users():
19
  """List all files (users) with saved states."""
@@ -25,10 +24,10 @@ def main():
25
  # Sidebar for selecting a user file
26
  st.sidebar.title("Saved Users")
27
  saved_users = list_saved_users()
28
- selected_user = st.sidebar.selectbox("Select a user", options=saved_users)
29
 
30
- # Load data for the selected user
31
- if selected_user:
32
  cached_data = load_user_data(selected_user)
33
  else:
34
  cached_data = {'email': '', 'phone': '', 'password': ''}
@@ -36,31 +35,30 @@ def main():
36
  # Input fields with emojis
37
  new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
38
  new_phone = st.text_input("πŸ“± Mobile Phone", value=cached_data['phone'])
39
-
40
- # Password field with an option to view contents
41
  show_password = st.checkbox("Show password")
42
  if show_password:
43
  new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'])
44
  else:
45
  new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'], type='password')
46
 
47
- # Update and save data if changes are made
48
- if new_email != cached_data['email'] or new_phone != cached_data['phone'] or new_password != cached_data['password']:
49
  cached_data = {'email': new_email, 'phone': new_phone, 'password': new_password}
50
  save_user_data(new_email, cached_data)
51
- st.success("Data updated and saved!")
52
 
53
  st.write("Current Data:")
54
  st.json(cached_data)
55
 
56
- # Password Reset Simulation
57
- if st.sidebar.button("Reset Password"):
58
- reset_email = st.sidebar.text_input("Enter email for password reset")
59
- if reset_email in saved_users:
60
- reset_data = load_user_data(reset_email)
61
- reset_data['password'] = 'new_password' # Reset password
62
- save_user_data(reset_email, reset_data)
63
- st.sidebar.success(f"Password reset for {reset_email}")
64
 
65
- if __name__ == "main":
66
- main()
 
 
8
  file.write(json.dumps(data))
9
 
10
  def load_user_data(email):
11
+ """Load user data from a file named after the user's email, or create a new file if it doesn't exist."""
12
+ if not os.path.isfile(f"{email}.txt"):
 
 
 
13
  return {'email': '', 'phone': '', 'password': ''}
14
+ with open(f"{email}.txt", "r") as file:
15
+ return json.loads(file.read())
16
 
17
  def list_saved_users():
18
  """List all files (users) with saved states."""
 
24
  # Sidebar for selecting a user file
25
  st.sidebar.title("Saved Users")
26
  saved_users = list_saved_users()
27
+ selected_user = st.sidebar.selectbox("Select a user", options=['New User'] + saved_users)
28
 
29
+ # Load or initialize data
30
+ if selected_user and selected_user != 'New User':
31
  cached_data = load_user_data(selected_user)
32
  else:
33
  cached_data = {'email': '', 'phone': '', 'password': ''}
 
35
  # Input fields with emojis
36
  new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
37
  new_phone = st.text_input("πŸ“± Mobile Phone", value=cached_data['phone'])
 
 
38
  show_password = st.checkbox("Show password")
39
  if show_password:
40
  new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'])
41
  else:
42
  new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'], type='password')
43
 
44
+ # Save data when changes are made
45
+ if new_email and (new_email != cached_data['email'] or new_phone != cached_data['phone'] or new_password != cached_data['password']):
46
  cached_data = {'email': new_email, 'phone': new_phone, 'password': new_password}
47
  save_user_data(new_email, cached_data)
48
+ st.sidebar.success("Data updated and saved!")
49
 
50
  st.write("Current Data:")
51
  st.json(cached_data)
52
 
53
+ # Password Reset Simulation
54
+ if st.sidebar.button("Reset Password"):
55
+ reset_email = st.sidebar.text_input("Enter email for password reset")
56
+ if reset_email and reset_email in saved_users:
57
+ reset_data = load_user_data(reset_email)
58
+ reset_data['password'] = 'new_password' # Reset password
59
+ save_user_data(reset_email, reset_data)
60
+ st.sidebar.success(f"Password reset for {reset_email}")
61
 
62
+ # Run the app
63
+ if __name__ == "__main__":
64
+ main()