NathanChen198 / streamlit-cookies-controller

Control client browser cookie for the site.
MIT License
31 stars 1 forks source link

Maybe doesn't work? #2

Open JamesDConley opened 6 months ago

JamesDConley commented 6 months ago

Maybe I am misunderstanding how to use this- but I am unable to get cookies to persist on my browser

I put this at the start of my streamlit app

controller = CookieController()
user_id = controller.get('user_id')
if user_id is None:
    controller.set("user_id", str(uuid4()))
user_id = controller.get('user_id')

And I am getting new UUIDs each time I refresh the page. Seems like the cookies are getting cleared on refresh ( I get new UUIDs each time I refresh )

I've tried setting some different settings, most recently same_site="lax", secure=False, partitioned=True, max_age=999999999999999 but I always get the same result (different UUIDS on refresh)

nsouch commented 5 months ago

Same here. I got the following test code from Streamlit Forum but getAll() is misleading since it doesn't fetch cookies from browser (just buffered return). Then I tried with a refresh (see after) but raises streamlit.errors.DuplicateWidgetID: There are multiple widgets with the same key='cookies'

controller = CookieController()

# Set a cookie
controller.set('cookie_name', 'testing')

# Get all cookies
# controller.refresh()
cookies = controller.getAll()

# Get a cookie
cookie = controller.get('cookie_name')

# Remove a cookie
controller.remove('cookie_name')

Nota: while running under debugger, the cookie appeared once in the browser... but not repeatable.

jcislinsky commented 5 months ago

+1

eli103 commented 4 months ago

For me, it seem neither the getAll() nor get() picked up any thing from cookie. It's weird .

smboy commented 4 months ago

I'm also seeing very weird behavior (especially with multipage app). During logout, I do

token_expiry(email)
if controller.get('c_user'):
    controller.remove('c_user')
st.info("Logged out successfully!")
st.switch_page("Home.py")

And below is a repeated error I see even when I explicitly set the key (controller.set('c_user', email))

  File "/usr/local/lib/python3.12/site-packages/streamlit_cookies_controller/cookie_controller.py", line 163, in remove
    self.__cookies.pop(name)
KeyError: 'c_user'

There are multiple other cases noted as well where this doesn't seem to work consistently. Sometimes getAll() doesn't even print anything. What is equally weird is when I print

st.write(st.session_state.get('cookies'))

prints cookies with c_user (key) But when I print below, both donot return anything.

st.write(controller.getAll())
st.write(controller.get('c_user'))
duolabmeng6 commented 3 months ago

I'm also having problems getting a cookie.

duolabmeng6 commented 3 months ago

unavailable

import streamlit as st
from streamlit_cookies_controller import CookieController

# 设置页面配置
st.set_page_config('Cookie QuickStart', '🍪', layout='wide')

# 初始化 Cookie 控制器
controller = CookieController()

# 检查是否已登录
def check_login():
    cookies = controller.getAll()
    return 'username' in cookies

# 显示欢迎信息
def welcome_user():
    cookies = controller.getAll()
    username = cookies.get('username', 'Guest')
    st.write(f"Welcome to Streamlit Cookie Controller :cookie: {username}")

# 登录表单
def login_form():
    st.title("欢迎访问 Streamlit Cookie Controller :cookie:")
    username = st.text_input("用户名")
    password = st.text_input("密码", type="password")
    if st.button("登录"):
        if username and password:
            # 简单验证(例如,用户名和密码是否都不为空)
            controller.set('username', username)
            st.experimental_rerun()

# 主逻辑
if check_login():
    welcome_user()
    if st.button("注销"):
        controller.remove('username')
        st.experimental_rerun()
else:
    login_form()
duolabmeng6 commented 3 months ago

I solved it. https://github.com/duolabmeng6/duolabmeng6.github.io/issues/2