Spaces:
Sleeping
Sleeping
import streamlit as st | |
import uuid | |
def get_user_id(): | |
if "user_id" not in st.session_state: | |
# JavaScript to check if 'user_id' exists in localStorage; if not, set it | |
st.components.v1.html( | |
""" | |
<script> | |
let userId = localStorage.getItem('user_id'); | |
if (!userId) { | |
userId = '""" + str(uuid.uuid4()) + """'; | |
localStorage.setItem('user_id', userId); | |
} | |
window.parent.postMessage(userId, "*"); | |
</script> | |
""", | |
height=0, | |
) | |
# Capture user_id from the message | |
st.session_state.user_id = st.query_params().get("user_id", [None])[0] | |
return st.session_state.user_id | |
user_id = get_user_id() | |
st.write("Your user ID:", user_id) | |