from/to the client browser.
It use universal-cookie package to access the cookies.
Installation
Open a terminal and run:
pip install streamlit-cookies-controller
Quickstart
Create a new file example.py
from streamlit_cookies_controller import CookieController
st.set_page_config('Cookie QuickStart', '🍪', layout='wide')
controller = CookieController()
# Set a cookie
controller.set('cookie_name', 'testing')
# Get all cookies
cookies = controller.getAll()
# Get a cookie
cookie = controller.get('cookie_name')
# Remove a cookie
controller.remove('cookie_name')
I have verified and it is not infinite loops.
It is rendering as expected on streamlit v1.31.1 when component height is zero.
Somehow streamlit 1.32.0 seem to flicker ‘Test1’ text when the component height is zero. Thus the illusion of infinite loop.
streamlit-cookies-controller have a helper function to RemoveEmptyElementContainer. That will hide div element when the iframe height is zero.
import streamlit as st
from streamlit_cookies_controller import CookieController, RemoveEmptyElementContainer
st.set_page_config('Cookie QuickStart', '🍪', layout='wide')
controller = CookieController()
RemoveEmptyElementContainer()
# to check how many time
if 'count' not in st.session_state:
st.session_state['count'] = 0
st.session_state['count'] += 1
count = st.session_state['count']
st.write(f"Total script run count: {count}")
# Set a cookie
controller.set('cookie_name', 'testing')
st.write(st.session_state)
# Get all cookies
cookies = controller.getAll()
st.write(cookies)
# Get a cookie
cookie = controller.get('cookie_name')
# Remove a cookie
controller.remove('cookie_name')
import streamlit as st
from streamlit import session_state as ss
from streamlit_cookies_controller import CookieController
import time
cookie_name = st.secrets['COOKIE_NAME']
controller = CookieController(key='cookies')
# Newly opened app or user reloads the page.
if 'login_ok' not in ss:
# Check the contents of cookie.
cookies = controller.getAll()
time.sleep(1)
# Get cookie username and password if there is.
cookie_username = controller.get(f'{cookie_name}_username')
cookie_password = controller.get(f'{cookie_name}_password')
if cookie_username and cookie_password:
ss.login_ok = True
ss.username = cookie_username
ss.password = cookie_password
st.success(f'Welcome back {ss.username}!!')
else:
ss.login_ok = False
Saving credentials to cookie after successful log in.
Great component!
But I get an error when I create a cookie (though the cookie is saved correctly):
2024-04-09 09:37:16.295 ComponentRequestHandler: GET C:\DEV_Streamlit\cookies\.venv\Lib\site-packages\streamlit_cookies_controller\frontend\build\bootstrap.min.css.map read error
Traceback (most recent call last):
File "C:\DEV_Streamlit\cookies\.venv\Lib\site-packages\streamlit\web\server\component_request_handler.py", line 54, in get
with open(abspath, "rb") as file:
^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\DEV_Streamlit\\cookies\\.venv\\Lib\\site-packages\\streamlit_cookies_controller\\frontend\\build\\bootstrap.min.css.map'
I also get a GUI problem:
before click the button “Set a cookie”
After the cookie is created (with the terminal error as response):
Thanks for informing the errors.
I have fixed the terminal error in ver 0.0.4.
I can’t duplicate the GUI error. Can send me your “Cookie Quickstart” code?
GUI error it seems is just the div with component’s iframe child showing itself when the cookie is set. Just add this to you code and it should be fine:
Is there any way to use Streamlit-Cookies-Controller with st-paywall to keep the user logged in after the page is refreshed? Would be nice if anyone can help with that! Thank you in advance
I would like to ask a question about how to configure a ‘cookie path’. I tried to add a multi page path, but it didn’t work. Perhaps I misunderstood. Can you give me an example? Thank you very much!
I’m seeing a very weird issue - perhaps a delay of 10s of seconds setting the cookie or cookie is not set ( I have to login again multiple times). I’m storing the login email as the cookie [ controller.set('c_user', email, max_age=24*60*60) ]
However, this doesn’t seem to be working all the time. Perhaps, not sure if I’m missing any other setting - will raise a query/request on the controller github page.