Streamlit Theme
A component that returns the active theme of the Streamlit app.
Installation
pip install st-theme
Usage
The function immediately returns the active theme, when it is called. If the user manually changes the theme, after the web app is already running, it updates the returned value.
Parameters
adjust : bool, default=True
If set to True, which is the default, it makes a CSS adjustment and removes
a space that would otherwise be added to the page by calling the st_theme
function.
Streamlit components are meant to render something in the web app, and
Streamlit adds a space for them even when there is nothing to render. Since
st_theme does not render anything, and only communicates with the frontend
to fetch the active theme, it makes a CSS adjustment to remove this space.
In most cases, the CSS adjustment does not interfere with the rest of the web
app, however there could be some situations where this occurs. If this happens,
or it is desired to disable it, pass False to adjust and, when necessary,
make your own CSS adjustment with st.html.
key : str or int, optional
A string or integer to use as a unique key for the component. Multiple
st_themes may not share the same key. Defaults to None.
Returns
theme : dict of {str : str} or None
A dictionary with the style settings being used by the active theme of the
Streamlit app, or None, if for some reason it could not be fetched.
Notes
There is a known bug, that depends on the browser, where the theme is not returned immediately when the function is called. But it is returned normally when the user changes it.
This can be a problem in determining the initial theme of the web app. Because, by default, Streamlit uses the user's operating system setting (which might be unknown) to automatically apply the light or dark mode to the app, when it is first rendered.
To solve the issue, it is recommended to set a
default theme configuration
for the app, and use its value in case of st_theme returning None.
Examples
A basic example:
import streamlit as st
from streamlit_theme import st_theme
theme = st_theme()
st.write(theme)
An example showing the CSS adjustment made, when set to True:
import streamlit as st
from streamlit_theme import st_theme
adjust = st.toggle("Make the CSS adjustment")
st.write("Input:")
st.code(
f"""
st.write("Lorem ipsum")
st_theme(adjust={adjust})
st.write("Lorem ipsum")
"""
)
st.write("Output:")
st.write("Lorem ipsum")
st_theme(adjust=adjust)
st.write("Lorem ipsum")
Requirements
To use this Streamlit component in your app, you will need:
- Python 3.8+
- Streamlit 1.20+
- The CSS adjustment depends on the browser compatibility with the :has pseudo-class
Development
Ensure you have Python 3.8+, Node.js and npm installed.
- Clone this repository:
git clone git@github.com:gabrieltempass/streamlit-theme.git
- Go to the
frontenddirectory and initialize and run the component template frontend:
cd streamlit-theme/streamlit_theme/frontend
npm install
npm run dev
- From a separate terminal, go to the repository root directory, create a new Python virtual environment, activate it and install Streamlit and the template as an editable package:
cd streamlit-theme
python3 -m venv venv
. venv/bin/activate
pip install streamlit
pip install -e .
Still from the same separate terminal, run the example Streamlit app:
streamlit run streamlit_theme/example.py
If all goes well, you should see something like this:
Modify the frontend code at
streamlit_theme/frontend/src/StTheme.vue.
Modify the Python code at streamlit_theme/__init__.py.
References
This Streamlit component was made with the streamlit-component-vue-vite-template, by @gabrieltempass.
Metadata
Release files for st-theme 1.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| st-theme-1.2.3.tar.gz | 73.9 kB | Without provenance | Details for st-theme-1.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| st_theme-1.2.3-py3-none-any.whl | Python 3 | none | any | Details for st_theme-1.2.3-py3-none-any.whl |
Total release size: 149.1 kB
Release files / st-theme-1.2.3.tar.gz
| Download URL | st-theme-1.2.3.tar.gz |
|---|---|
| Size | 73.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ca97aece1a48ded6e83fd742c27cb0851e1bce2100ab4b6c37c7b6e003b65b42
|
|
BLAKE2b-256 checksum How to use checksums |
31843e2fd95f5acaf6c92a51e7f038f1120235c65bb0b8be9bf43aa9fb4267e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.11
|
Release files / st_theme-1.2.3-py3-none-any.whl
| Download URL | st_theme-1.2.3-py3-none-any.whl |
|---|---|
| Size | 75.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a54d9817dd5f8a6d7b0d071b25ae72eacf536c63a5fb97374923938021b1389
|
|
BLAKE2b-256 checksum How to use checksums |
3009cd7134f1ed5074a7d456640e7ba9a8c8e68a831837b4e7bfd9f29e5700a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.11
|
Security for st-theme 1.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.