File size: 828 Bytes
be196d0
68cb778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5821d03
46f7fb9
68cb778
3ba0366
68cb778
 
46f7fb9
68cb778
 
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
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)