gursi26 commited on
Commit
a887238
·
1 Parent(s): 0078aeb

trying again for user id

Browse files
Files changed (2) hide show
  1. app.py +18 -16
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,32 +1,34 @@
1
  import streamlit as st
2
  import uuid
3
- from streamlit_cookies_manager import CookieManager
 
4
 
5
- # Initialize the cookie manager without encryption
6
- cookies = CookieManager(
7
- prefix="my_app_" # You can use any prefix
8
- )
9
 
10
- # Ensure the cookies are ready
11
- if not cookies.ready():
12
- st.stop()
13
 
14
- def get_user_id():
15
- if 'user_id' not in cookies:
 
 
 
 
 
 
16
  # Generate a new UUID
17
  user_id = str(uuid.uuid4())
18
- # Store it in cookies
19
- cookies['user_id'] = user_id
20
- cookies.save() # Save the cookies
21
- else:
22
- # Retrieve the user_id from cookies
23
- user_id = cookies['user_id']
24
  return user_id
25
 
26
  def main():
27
  st.title("Unique User ID with Persistent UUID")
28
 
29
  user_id = get_user_id()
 
30
  st.write(f"Your user ID is: `{user_id}`")
31
 
32
  if __name__ == '__main__':
 
1
  import streamlit as st
2
  import uuid
3
+ import extra_streamlit_components as stx
4
+ from datetime import datetime
5
 
6
+ def get_user_id():
7
+ # Initialize CookieManager
8
+ cookie_manager = stx.CookieManager()
 
9
 
10
+ # Get all cookies
11
+ cookies = cookie_manager.get_all()
 
12
 
13
+ if not cookies:
14
+ # Cookies are not ready yet
15
+ st.write("Loading user ID...")
16
+ st.stop()
17
+
18
+ user_id = cookies.get('user_id')
19
+
20
+ if user_id is None:
21
  # Generate a new UUID
22
  user_id = str(uuid.uuid4())
23
+ # Set the cookie (expires in 10 years)
24
+ cookie_manager.set('user_id', user_id, expires_at=datetime.fromisoformat("2033-01-01T00:00:00"))
 
 
 
 
25
  return user_id
26
 
27
  def main():
28
  st.title("Unique User ID with Persistent UUID")
29
 
30
  user_id = get_user_id()
31
+
32
  st.write(f"Your user ID is: `{user_id}`")
33
 
34
  if __name__ == '__main__':
requirements.txt CHANGED
@@ -2,4 +2,4 @@ paho-mqtt==2.1.0
2
  pillow==10.4.0
3
  setuptools==75.1.0
4
  wheel==0.44.0
5
- streamlit-cookies-manager==0.2.0
 
2
  pillow==10.4.0
3
  setuptools==75.1.0
4
  wheel==0.44.0
5
+ extra-streamlit-components==0.1.71