An interface to access user session context.
st.context provides a read-only interface to access headers and cookies for the current user session.
Each property (st.context.headers and st.context.cookies) returns a dictionary of named values.
Class description[source] | |
---|---|
st.context() | |
Attributes | |
A read-only, dict-like object containing cookies sent in the initial request. | |
A read-only, dict-like object containing headers sent in the initial request. | |
The read-only IP address of the user's connection. | |
Whether the app is embedded. | |
The read-only locale of the user's browser. | |
The read-only timezone of the user's browser. | |
The read-only timezone offset of the user's browser. | |
The read-only URL of the app in the user's browser. |
Function signature[source] | |
---|---|
context.cookies |
Examples
Example 1: Access all available cookies
Show a dictionary of cookies:
import streamlit as st st.context.cookies
Example 2: Access a specific cookie
Show the value of a specific cookie:
import streamlit as st st.context.cookies["_ga"]
A read-only, dict-like object containing headers sent in the initial request.
Keys are case-insensitive and may be repeated. When keys are repeated, dict-like methods will only return the last instance of each key. Use .get_all(key="your_repeated_key") to see all values if the same header is set multiple times.
Function signature[source] | |
---|---|
context.headers |
Examples
Example 1: Access all available headers
Show a dictionary of headers (with only the last instance of any repeated key):
import streamlit as st st.context.headers
Example 2: Access a specific header
Show the value of a specific header (or the last instance if it's repeated):
import streamlit as st st.context.headers["host"]
Show of list of all headers for a given key:
import streamlit as st st.context.headers.get_all("pragma")
The read-only IP address of the user's connection.
This should not be used for security measures because it can easily be spoofed. When a user accesses the app through localhost, the IP address is None. Otherwise, the IP address is determined from the remote_ip attribute of the Tornado request object and may be an IPv4 or IPv6 address.
Function signature[source] | |
---|---|
context.ip_address |
Example
Check if the user has an IPv4 or IPv6 address:
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.")
Whether the app is embedded.
This property returns a boolean value indicating whether the app is running in an embedded context. This is determined by the presence of embed=true as a query parameter in the URL. This is the only way to determine if the app is currently configured for embedding because embedding settings are not accessible through st.query_params or st.context.url.
Function signature[source] | |
---|---|
context.is_embedded |
Example
Conditionally show content when the app is running in an embedded context:
import streamlit as st if st.context.is_embedded: st.write("You are running the app in an embedded context.")
The read-only locale of the user's browser.
st.context.locale returns the value of navigator.language from the user's DOM. This is a string representing the user's preferred language (e.g. "en-US").
Function signature[source] | |
---|---|
context.locale |
Example
Access the user's locale to display locally:
import streamlit as st if st.context.locale == "fr-FR": st.write("Bonjour!") else: st.write("Hello!")
Function signature[source] | |
---|---|
context.timezone |
Example
Access the user's timezone, and format a datetime to display locally:
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)}"
Function signature[source] | |
---|---|
context.timezone_offset |
Example
Access the user's timezone offset, and format a datetime to display locally:
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)}"
The read-only URL of the app in the user's browser.
st.context.url returns the URL through which the user is accessing the app. This includes the scheme, domain name, port, and path. If query parameters or anchors are present in the URL, they are removed and not included in this value.
Function signature[source] | |
---|---|
context.url |
Example
Conditionally show content when you access your app through localhost:
import streamlit as st if st.context.url.startswith("http://localhost"): st.write("You are running the app locally.")
Still have questions?
Our forums are full of helpful information and Streamlit experts.