This is amazing @MarceTU . Thank you for posting this. Was looking into http cookies, or headers from Streamlit apps for ages! This just made my week! Cheers!!
in cloud, I can’t seem to get any of the cookies to work…
I’ve put the cookie manager example as a streamlit app and looks like you can’t set cookies on Cloud?
Well the definition of cloud is broad. But if you specifically mean share.streamlit.io, which its on device cookies are accessible by your application same to other’s, then it’s a security issue. Which I am not sure if it’s allowed anymore to do on share.streamlit.io.
However if you host your Streamlit application on a domain/subdomain only accessible by your application then it shall not be an issue and you can easily set cookies using cookie manager.
TLDR; It’s highly not advised to set user cookies on share.streamlit.io
ah i see. I’ll try again on GCP or Heroku or something…thank you
I’m running into troubles with the CookieManager and getting cookies. I can successfully add cookies to the browser - I can see them in the developer tools. However, when I try getting the cookie, all I get is None. Thoughts, suggestions?
import extra_streamlit_components as stx
import streamlit as st
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def get_cookie_manager():
return stx.CookieManager()
cookie_manager = get_cookie_manager()
cookie_name = "Cookie Test"
cookie_value = cookie_manager.get(cookie=cookie_name)
print(f"Cookie value: {cookie_value}")
if cookie_value is None:
cookie_value = ""
with st.form(key="Cookie"):
cookie_value = st.text_input(label="Cookie value:", value=cookie_value)
submitted = st.form_submit_button("Submit")
if submitted:
print(f"Submitting: {cookie_value}")
cookie_manager.set(cookie=cookie_name, val=cookie_value)
print(f"After set: {cookie_manager.get(cookie=cookie_name)}")
I figured out the problem. I had to change the cookies settings in Chrome to “Allow all cookies”.
However, I still have a problem. When running my streamlit app I can refresh the browser and my cookie will be retrieved. However, if I stop and restart my streamlit app, the cookie isn’t found despite it showing up in the cookies in the developer tools.
it is not working with on_click
Hey there, thanks for making this. I’m having some problems with st.error
, st.status
, or st.success
alongside cookies; here is a minimum reproducible example with streamlit 1.10.0
import streamlit as st
import extra_streamlit_components as stx
@st.cache(allow_output_mutation=True)
def get_manager():
return stx.CookieManager()
cookie_manager = get_manager()
button = st.button("Get cookies")
if button:
st.subheader("All Cookies:")
cookies = cookie_manager.get_all()
st.write(cookies)
st.success("This should show up for longer than a split second")
The green st.success
box shows up for only a split second. Any help is much appreciated!
Edit: After posting this, I realized an example above worked fine using forms. As such, here is an extraordinarily hacky way to solve this bug.
import streamlit as st
import extra_streamlit_components as stx
@st.cache(allow_output_mutation=True)
def get_manager():
return stx.CookieManager()
cookie_manager = get_manager()
with st.form(key="Cookie"):
hide_streamlit_style = """
<style>
[data-testid="stForm"] {border: none; padding: 0;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
submitted = st.form_submit_button("Get cookies")
if submitted:
st.subheader("All Cookies:")
cookies = cookie_manager.get_all()
st.write(cookies)
st.success("This should show up for longer than a split second")
I’ve created a function that gets all cookies from the client, including HTTP-only cookies. The other functions posted by the users get all the cookies from all the sessions connected to the streamlit server. This is bad for security issues if you search for authentication cookies. Here I have a function that gets only the cookies for the client:
import re
from streamlit.server.server import Server
from streamlit.scriptrunner import add_script_run_ctx
def get_cookies() -> dict:
session_id = add_script_run_ctx().streamlit_script_run_ctx.session_id
session_info = Server.get_current()._get_session_info(session_id)
header = session_info.ws.request.headers
header = dict(header.get_all())
cookies_str = header["Cookie"]
results = re.findall(r"([\w]+)=([^;]+)", cookies_str)
cookies = dict(results)
return cookies
I cannot make it work,
I see cookies in the browser but none is returned when trying to get them
This streamlit extension is awesome !
when I tried implementing in my app, it caused all sorts of unexpected behavior and was breaking my scripts. Turns out that calling cookie_manager causes the app.py script to run multiple times, and returns the cookies only on the last run. To test, I used session state to track the number of runs and the result:
@st.cache(allow_output_mutation=True)
def get_manager():
return stx.CookieManager()
cookie_manager = get_manager()
cookies = cookie_manager.get_all()
if 'counter' not in st.session_state:
st.session_state['counter'] = 0
st.session_state['result'] = {}
st.session_state['counter'] = st.session_state['counter'] + 1
st.session_state['result'][st.session_state['counter']] = cookies
st.write(st.session_state['result'])
Result:
As you can see, the full script ran top to bottom 3 times before finally returning the cookies. That was the first run after starting the server. If I refresh the page, it will only run twice.
If I change “cookies = cookie_manager.get_all()” to "cookies = {‘my’: ‘cookie’}, the script only runs once as expected:
Is this the intended behavior? I’m guessing this is the reason many are having trouble getting it to work.
Same here. Cannot make it work. Cookies are there, but they are not returned. Occasionally it works.
see my post just above. when get_manager() is called, the script is trigger to run multiple times, it doesn’t return the cookies until the second or third pass. You need to write your script in such a way that it waits until the cookies are returned until proceeding.
I’m having a similar experience as many other users reporting here. I too experience the 3x loading as described and inconsistent return of cookies despite trying different ways of trying to get around that.
It’s effectively unusable for me.
Note I have found more luck with streamlit-cookies-manager · PyPI
But this library does not work on safari (macOS or iOS) in my testing. There are some issues with it sometimes initially loading as well.
Hi,
I wrote a set of functions to get server properties, client headers and client cookies.
They use HACKS discussed in these threads:
- _get_websocket_headers() by tconkling · Pull Request #5457 · streamlit/streamlit · GitHub
- Streamlit.script_run_context() gone with 1.8?
Please note: this code is unsupported and will break if the Streamlit dev team change their APIs. You’re on your own
import re
import streamlit as st
try:
# Streamlit >= 1.12.0
from streamlit.web.server.server import Server
from streamlit.runtime.runtime import Runtime, SessionInfo
from streamlit.runtime.scriptrunner import add_script_run_ctx
# from streamlit.runtime.scriptrunner import get_script_run_ctx
except:
raise Exception('You must use Streamlit >= v1.12.0')
# Mega hack walking the GC heap.
# Look only for singletons that you know exist for the entire streamlit server lifetime, e.g.: Server and Runtime!!
def st_instance_of_type(type_obj: object) -> object:
import gc
st_obj = None
for obj in gc.get_objects():
if type(obj) is type_obj:
st_obj = obj
break
return st_obj
def st_server_props():
st_server = st_instance_of_type(Server)
st_server_runtime = st_server._runtime
st_gc_runtime = st_instance_of_type(Runtime)
assert(st_server_runtime == st_gc_runtime)
main_script_path = st_server.main_script_path
browser_is_connected = st_server.browser_is_connected
return {'st_server_runtime': st_server_runtime, 'st_gc_runtime': st_gc_runtime, 'main_script_path': main_script_path, 'browser_is_connected': browser_is_connected}
def st_session_info() -> SessionInfo:
st_runtime = st_instance_of_type(Runtime)
# get session id from the current script runner thread
session_id = add_script_run_ctx().streamlit_script_run_ctx.session_id
# use the session id to retrieve the session info
session_info = st_runtime._get_session_info(session_id)
return session_info
def st_client_headers() -> dict:
session_info = st_session_info()
client_headers = session_info.client.request.headers._dict
return dict(client_headers)
def st_client_cookies() -> dict:
client_headers = st_client_headers()
cookies_str = client_headers["Cookie"]
results = re.findall(r"([\w]+)=([^;]+)", cookies_str)
cookies = dict(results)
return cookies
st.subheader('Server Props')
st.write(st_server_props())
st.subheader('Client Headers')
st.write(st_client_headers())
st.subheader('Client Cookies')
st.write(st_client_cookies())
HTH,
Arvindra
This is a good method. Unfortunately, it can only query the current session ID. Once I change the cookie, it still returns the previous value. Thank you
lucky! I found an imperfect method, but for no one, it can already be applied in practice.
# Js test
from streamlit_js_eval import get_cookie, set_cookie
from random import randint
# Trigger button
def refresh(): # Refresh Page
pass
st.button('Refresh', on_click=refresh)
# Trigger mediation rules (important)
if not 'abc' in st.session_state: #
st.session_state['get_cookie_sign'] = 0
st.session_state['get_cookie1'] = 'cookie1'
st.session_state['get_cookie2'] = 'cookie11'
st.session_state['get_cookie3'] = 'cookie111'
st.session_state['abc'] = 0
elif st.session_state['abc'] == 1: #
st.session_state['get_cookie_sign'] = 1
st.session_state['get_cookie1'] = 'cookie2'
st.session_state['get_cookie2'] = 'cookie21'
st.session_state['get_cookie3'] = 'cookie211'
st.session_state['abc'] = 2
else: #
match st.session_state['get_cookie_sign']:
case 0:
st.session_state['abc'] += 1
case 1:
del st.session_state.abc
st.write('session_state:')
st.write(st.session_state)
st.session_state['get_cookie_sign'] = 2
# set cookie
print(100)
set_cookie(name='test1', value=randint(0, 100), duration_days=1, component_key='012')
set_cookie(name='test2', value=randint(0, 100), duration_days=1, component_key='013')
set_cookie(name='test3', value=randint(0, 100), duration_days=1, component_key='014')
# get cookie
print(200)
cookie = []
cookie.append(get_cookie(name='test1', component_key=st.session_state['get_cookie1']))
cookie.append(get_cookie(name='test2', component_key=st.session_state['get_cookie2']))
cookie.append(get_cookie(name='test3', component_key=st.session_state['get_cookie3']))
# Printout
st.write('cookie:')
st.write(cookie)
# Main program interception
if st.session_state['get_cookie_sign'] != 2:
print('停止?')
st.stop()
# Add main program after here
print(300)
Reference source aghasemi/streamlit_js_eval:一个自定义的 Streamlit 组件,用于评估任意 Javascript 表达式 (github.com)
If you want to delete cookies, please use stx. CookieManager()
I have optimized the script logic in practical application, which may improve the running efficiency of the program and reduce unnecessary repeated running, but it may take some time, but I think it will be better than the previous scheme because it reduces the occasional repeated running, and now it looks more stable.
from streamlit_js_eval import get_cookie, set_cookie
from random import randint
# 触发按钮;在整个程序中必须存在按钮等被用户触发的组件,否则将导致程序异常循环
def shuaxin(): # 刷新页面
# 仅仅是为了触发st更新
pass
st.button('刷新', on_click=shuaxin)
# set cookie;设置cookie
set_cookie(name='test1', value=randint(0, 100), duration_days=1, component_key='012')
set_cookie(name='test2', value=randint(0, 100), duration_days=1, component_key='013')
set_cookie(name='test3', value=randint(0, 100), duration_days=1, component_key='014')
def cookie_obtain():
# 获取cookie
def cookie_get_cookie():
import time
st.session_state['cookies'] = {}
get_cookie(name='test1', component_key=st.session_state['get_cookie1']) # 如果没有得到响应信息,是None(并不是返回的)
get_cookie(name='test2', component_key=st.session_state['get_cookie2'])
get_cookie(name='test4', component_key=st.session_state['get_cookie3']) # test4 是不存在的,返回 ''
time.sleep(0.06) # 响应会有延迟,响应后会添加到会话(添加等待时间之后,不一定会执行重复运行,但是偶尔还会);可以按需要来调整等待时间,但最好不要小于0.05秒,0.1秒比较稳定
# 触发调解规则;重要的
if not 'abc' in st.session_state: # ①首次打开页面、用户F5刷新页面、执行②之后的用户触发st更新开始执行位置
st.session_state['get_cookie_sign'] = 0
st.session_state['get_cookie1'] = 'cookie1'
st.session_state['get_cookie2'] = 'cookie11'
st.session_state['get_cookie3'] = 'cookie111'
cookie_get_cookie() # 获取cookie,因为获取仍有失败的可能,失败后会立即重新运行
st.session_state['abc'] = 0
elif st.session_state['abc'] == 1: # ②用户触发st更新执行位置
st.session_state['get_cookie_sign'] = 1
st.session_state['get_cookie1'] = 'cookie2'
st.session_state['get_cookie2'] = 'cookie21'
st.session_state['get_cookie3'] = 'cookie211'
cookie_get_cookie() # 获取cookie,因为获取仍有失败的可能,失败后会立即重新运行
st.session_state['abc'] = 2
else: # ③ ①结束执行位置
match st.session_state['get_cookie_sign']:
case 0:
st.session_state['abc'] += 1
case 1:
del st.session_state.abc
st.session_state['cookies'] = {
'test1': st.session_state[st.session_state['get_cookie1']],
'test2': st.session_state[st.session_state['get_cookie2']],
'test4': st.session_state[st.session_state['get_cookie3']],
}
st.write('session_state:')
st.write(st.session_state)
st.session_state['get_cookie_sign'] = 2
# 打印输出
st.write('cookie:')
st.write(st.session_state['cookies'])
# 主程序截流
if st.session_state['get_cookie_sign'] != 2:
print('停止?')
st.stop()
else:
return st.session_state['cookies']
cookies = cookie_obtain()
# 在此处之后添加主程序
print('执行主程序')
Thanks again 'streamlit_ js_ Eval ', which makes cookie management practical