awacke1 commited on
Commit
b072499
Β·
verified Β·
1 Parent(s): 7017f6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -19
app.py CHANGED
@@ -1,18 +1,37 @@
1
  import streamlit as st
 
 
2
 
3
- @st.cache(suppress_st_warning=True,allow_output_mutation=True, ttl=86400) # 86400 seconds = 1 day
4
- #@st.cache_data(ttl=86400) # 86400 seconds = 1 day
5
- #@st.cache(suppress_st_warning=True, ttl=86400)
6
- #@st.cache_data(experimental_allow_widgets=True) # πŸ‘ˆ Set the parameter
7
- def cache_user_data():
8
- return {'email': '', 'phone': '', 'password': ''}
 
 
 
 
 
 
 
 
 
 
9
 
10
- #@st.cache_data(experimental_allow_widgets=True) # πŸ‘ˆ Set the parameter
11
  def main():
12
- st.title('User Data Caching Example')
13
 
14
- # Retrieve or initialize cached data
15
- cached_data = cache_user_data()
 
 
 
 
 
 
 
 
16
 
17
  # Input fields with emojis
18
  new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
@@ -25,16 +44,21 @@ def main():
25
  else:
26
  new_password = st.text_input("πŸ”‘ Password", value=cached_data['password'], type='password')
27
 
28
- # Update cache if data changes
29
  if new_email != cached_data['email'] or new_phone != cached_data['phone'] or new_password != cached_data['password']:
30
- cached_data['email'] = new_email
31
- cached_data['phone'] = new_phone
32
- cached_data['password'] = new_password
33
- st.success("Data updated and cached!")
34
 
35
- st.write("Cached Data:")
36
  st.json(cached_data)
37
 
38
- # Run the app
39
- if __name__ == "__main__":
40
- main()
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import json
4
 
5
+ def save_user_data(email, data):
6
+ """Save user data to a file named after the user's email."""
7
+ with open(f"{email}.txt", "w") as file:
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."""
20
+ return [f[:-4] for f in os.listdir() if f.endswith('.txt')]
21
 
 
22
  def main():
23
+ st.title('User Data Management')
24
 
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': ''}
35
 
36
  # Input fields with emojis
37
  new_email = st.text_input("πŸ“§ Email Address", value=cached_data['email'])
 
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
+