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 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.

A read-only, dict-like object containing cookies sent in the initial request.

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 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!")

The read-only timezone of the user's browser.

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)}"

The read-only timezone offset of the user's browser.

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)}"
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.