访问用户会话上下文的接口。

st.context提供只读接口,用于访问当前用户会话的请求头和 Cookie。

每个属性(st.context.headersst.context.cookies)都返回一个包含命名值的字典。

类描述[source]

st.context()

属性

一个只读的、类似字典的对象,包含初始请求中发送的 Cookie。

一个只读的、类似字典的对象,包含初始请求中发送的请求头。

用户的连接的只读 IP 地址。

应用是否嵌入。

用户浏览器的只读 locale(区域设置)。

用户浏览器的只读时区。

用户浏览器的只读时区偏移量。

用户浏览器中应用的只读 URL。

一个只读的、类似字典的对象,包含初始请求中发送的 Cookie。

函数签名[source]

context.cookies

示例

示例 1:访问所有可用的 Cookie

显示 Cookie 字典

import streamlit as st

st.context.cookies

示例 2:访问特定的 Cookie

显示特定 Cookie 的值

import streamlit as st

st.context.cookies["_ga"]

一个只读的、类似字典的对象,包含初始请求中发送的请求头。

键不区分大小写,并且可能重复。当键重复时,类似字典的方法只会返回每个键的最后一个实例。使用.get_all(key="your_repeated_key")可以查看同一请求头多次设置时的所有值。

函数签名[source]

context.headers

示例

示例 1:访问所有可用的请求头

显示请求头字典(仅包含重复键的最后一个实例)

import streamlit as st

st.context.headers

示例 2:访问特定的请求头

显示特定请求头的值(如果是重复的则显示最后一个实例)

import streamlit as st

st.context.headers["host"]

显示给定键的所有请求头值的列表

import streamlit as st

st.context.headers.get_all("pragma")

用户的连接的只读 IP 地址。

不应将此用于安全措施,因为它很容易被欺骗。当用户通过localhost访问应用时,IP 地址为None。否则,IP 地址由 Tornado 请求对象的 remote_ip 属性确定,可能是 IPv4 或 IPv6 地址。

函数签名[source]

context.ip_address

示例

检查用户是否具有 IPv4 或 IPv6 地址

import streamlit as st

ip = st.context.ip_address
if ip is None:
    st.write("No IP address. This is expected in local development.")
elif ip.contains(":"):
    st.write("You have an IPv6 address.")
elif ip.contains("."):
    st.write("You have an IPv4 address.")
else:
    st.error("This should not happen.")

应用是否嵌入。

此属性返回一个布尔值,指示应用是否在嵌入式上下文中运行。这取决于 URL 中是否存在embed=true作为查询参数。这是确定应用当前是否配置为嵌入的唯一方法,因为嵌入设置无法通过st.query_paramsst.context.url.

函数签名[source]

context.is_embedded

示例

当应用在嵌入式上下文中运行时有条件地显示内容

import streamlit as st

if st.context.is_embedded:
    st.write("You are running the app in an embedded context.")

用户浏览器的只读 locale(区域设置)。

st.context.locale返回用户 DOM 中 navigator.language 的值。这是一个表示用户首选语言的字符串(例如 "en-US")。

函数签名[source]

context.locale

示例

访问用户 locale 以进行本地化显示

import streamlit as st

if st.context.locale == "fr-FR":
    st.write("Bonjour!")
else:
    st.write("Hello!")

用户浏览器的只读时区。

函数签名[source]

context.timezone

示例

访问用户时区,并格式化日期时间以进行本地化显示

import streamlit as st
from datetime import datetime, timezone
import pytz

tz = st.context.timezone
tz_obj = pytz.timezone(tz)

now = datetime.now(timezone.utc)

f"The user's timezone is {tz}."
f"The UTC time is {now}."
f"The user's local time is {now.astimezone(tz_obj)}"

用户浏览器的只读时区偏移量。

函数签名[source]

context.timezone_offset

示例

访问用户时区偏移量,并格式化日期时间以进行本地化显示

import streamlit as st
from datetime import datetime, timezone, timedelta

tzoff = st.context.timezone_offset
tz_obj = timezone(-timedelta(minutes=tzoff))

now = datetime.now(timezone.utc)

f"The user's timezone is {tz}."
f"The UTC time is {now}."
f"The user's local time is {now.astimezone(tz_obj)}"

用户浏览器中应用的只读 URL。

st.context.url返回用户访问应用的 URL。这包括方案、域名、端口和路径。如果 URL 中存在查询参数或锚点,它们将被移除,不包含在此值中。

函数签名[source]

context.url

示例

通过以下方式访问应用时有条件地显示内容localhost:

import streamlit as st

if st.context.url.startswith("http://localhost"):
    st.write("You are running the app locally.")
forum

还有问题吗?

我们的 论坛 提供了大量有用的信息和 Streamlit 专家。