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.
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")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.