Finally I find a right way to use extra-cookie.manager!

Summary

The package Extra-Streamlit-Components provides capacity for cookie management. However, due to Streamlit’s re-running mechanism, a lot of challenges were encountered while implementing read and write operations for cookies. Eventually, I managed to find the correct way to call these operations. Under the stateless protocol of HTTP, in the mechanism of Streamlit, stable read, judge and write operations of cookies can be carried out efficiently.

Steps to reproduce

Code snippet:

cookie_manager = get_manager()
start_time = time.time()
timeout = 0.01  # Preset timeout time
while True:
    if 'cookies' not in st.session_state:
        st.session_state['cookies'] = cookie_manager.get_all()
    elif time.time() - start_time > timeout:
        st.warning("Operation timeout. Please refresh the page or check your network connection.")
        break  # If timed out, exit the loop directly
    else:
        if 'lzs_userid' not in st.session_state.cookies:
            if 'lzs_userid' not in st.session_state:
                st.session_state["lzs_userid"] = str(uuid.uuid4())
            cookie_manager.set('lzs_userid', st.session_state["lzs_userid"], key="0", expires_at=datetime.datetime(year=2023, month=8, day=2))
        if "lzs_pwd" not in st.session_state.cookies:
            if "lzs_pwd" not in st.session_state:
                random_pwd = ''.join(random.choices(string.ascii_letters + string.digits,k=8))
                st.session_state['lzs_pwd'] = bcrypt.hashpw(random_pwd.encode(),bcrypt.gensalt(rounds=10)).decode('utf-8')
            cookie_manager.set('lzs_pwd', st.session_state.get('lzs_pwd', ''), key="1", expires_at=datetime.datetime(year=2023, month=8, day=2))
        break

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Explain what you expect to happen when you run the code above.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

2 Likes

@Hangjun_Ji Thanks for sharing!

The trick is simply to insert a time.sleep() after running
cookie_manager.get_all(), this gives it enough time to capture the cookies…

Havn’t looked at the library code, but feel like it is probably run asynchronous?

2 Likes

Vow! This is true! I had an issue where fleetingly I would not find the cookies… And then it would suddenly appear after a few seconds…
So my streamlit app would fleetingly display “access denied” and then immediately load the page… So weird!!
I added a time.sleep(2) that fixed it for me. (time.sleep(1) still had issues for my setup. your mileage could vary)
Thank you lorenzweb!

Where can I put time.sleep(2) in this code? I have tried different ways but does not work:

    authorization_url = asyncio.run(
        write_authorization_url(client=client,
                                redirect_uri=redirect_uri)
    )
    test = cookie_manager.get_all()
    if "credentials" not in test:
        print("hi")
        try:
            code = st.experimental_get_query_params().get('code')
            if code is None:
                # Si no hay código, muestra un enlace para iniciar sesión
                st.write(f'''<h1>Please login using this <a target="_self"
                            href="{authorization_url}">url</a></h1>''',
                            unsafe_allow_html=True)
            else:
                try:
                    token = asyncio.run(
                        write_access_token(client=client,
                                        redirect_uri=redirect_uri,
                                        code=code))
                    if token:
                        print(token['id_token'])
                        id_token = token['id_token']
                        cookie_manager.set("credentials", id_token)
                except Exception as e:
                    st.write(f'''<h1>Error: {str(e)}. Please try again: <a target="_self"
                                href="{authorization_url}">url</a></h1>''',
                            unsafe_allow_html=True)
                else:
                    # Verifica si el token ha expirado
                    if token.is_expired():
                        # Si el token ha expirado, muestra un mensaje para volver a iniciar sesión
                        st.write(f'''<h1>Login session has ended, please <a target="_self"
                                    href="{authorization_url}">login</a> again.</h1>''')
                    else:
                        # Si todo está correcto, almacena el token y muestra la información del usuario
                        value = token
                        user_id, user_email = asyncio.run(
                            get_email(client=client,
                                    token=token['access_token'])
                        )
                        main(user_id=user_id,
                            user_email=user_email)
                        id_token = token['id_token']
                        cookie_manager.set("credentials", id_token)
        except Exception as e:
            time.sleep(1)
            # Captura excepciones generales y muestra un mensaje de error
            st.write(f'''<h1>Error: {str(e)}. Please try again: <a target="_self"
                        href="{authorization_url}">url</a></h1>''',
                    unsafe_allow_html=True)
    else:
        # Si ya hay un valor en las cookies, muestra la información del usuario
        main(user_id="usr", user_email="eml")

have you tried placing it after the line: test = cookie_manager.get_all()

1 Like

I think I tried that and it became very slow.

I then used a “undocumented, unsupported” way of accessing it. Use it at your risk! But it works cleanly and is much faster.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.