Streamlit Theme Component
st-theme is a Streamlit component that enables Python applications to retrieve the active theme settings (e.g., colors, font) of the Streamlit app. It is currently at version 1.2.3 and maintains an active development cadence with several minor releases per year, ensuring compatibility and bug fixes.
Warnings
- gotcha The `st-theme` console script, introduced in version 1.2.3, currently returns an error when used with Streamlit version 1.34.0 due to an upstream bug in Streamlit.
- gotcha Upon initial page load, `st_theme()` might return `None` due to a browser-dependent bug where the theme is not immediately available. It updates correctly when the user changes the theme.
- gotcha The `st_theme` function includes a `adjust` parameter (default `True`) that applies a CSS adjustment to remove extra space from the component. While generally safe, this CSS adjustment might, in rare cases, interfere with other elements of your Streamlit app.
- gotcha Be aware of other similarly named libraries like `streamlit-themes` (note the hyphen). The `st-theme` library (this entry) is for *retrieving* the active theme, while `streamlit-themes` focuses on *setting* themes and has its own caveats, including accessing internal Streamlit configurations which may break in future updates.
Install
-
pip install st-theme
Imports
- st_theme
from streamlit_theme import st_theme
Quickstart
import streamlit as st
from streamlit_theme import st_theme
st.set_page_config(layout="centered", page_title="Theme Demo")
st.title("Current Streamlit Theme")
st.markdown("--- ")
theme = st_theme()
if theme:
st.success("Theme successfully fetched!")
st.write("Here are the active theme settings:")
st.json(theme)
st.info(f"Primary Color: {theme.get('primaryColor', 'N/A')}")
st.info(f"Background Color: {theme.get('backgroundColor', 'N/A')}")
else:
st.warning("Theme could not be fetched immediately. This is a known browser-dependent issue upon initial load. Try changing the theme manually (light/dark mode) or refresh the page. A default theme is used as a fallback for display purposes.")
# Provide a default or handle the None case gracefully
default_theme = {
"base": "light",
"primaryColor": "#F63366",
"backgroundColor": "#FFFFFF",
"secondaryBackgroundColor": "#F0F2F6",
"textColor": "#262730",
"font": "sans serif"
}
st.write("Displaying a fallback default theme:")
st.json(default_theme)
st.info("Using a default light theme as a fallback for display.")