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:
Example 2: Access a specific cookie
Show the value of a specific cookie:
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):
Example 2: Access a specific header
Show the value of a specific header (or the last instance if it's repeated):
Show of list of all headers for a given key:
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:
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:
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:
Function signature[source] | |
---|---|
context.timezone |
Example
Access the user's timezone, and format a datetime to display locally:
Function signature[source] | |
---|---|
context.timezone_offset |
Example
Access the user's timezone offset, and format a datetime to display locally:
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:
Still have questions?
Our forums are full of helpful information and Streamlit experts.