TL;DR - How can I check if the app is in dark mode or light mode at runtime to match in-app theme to browser theme?
I’m creating a data app with a lot of custom styling in the visualizations. The issue I’m having is that the visuals look great in Light Mode but terrible in Dark Mode. I would like to develop a separate styling for each mode that can be applied based on which mode is active.
This can’t be done using the get_config_option utility because this (currently) only returns values from custom configurations, and I want to check which default theme configuration is being used.
Is there a way to do this?
Here is some pseudo-code of what I would like to do:
This is useful. For instance, I want to place a logo on my Streamlit app. If I could check the theme at runtime, I would use specific image files for light and dark modes.
E.g.:
if st.theme() == 'Dark':
st.image('dark_logo.png')
else:
st.image('light_logo.png')
Yes, it would be really helpful if st.theme() was implemented like you described! I keep running into this blocker when trying to build apps — if I’m developing custom code for my Streamlit app (but am not going as far as to build a whole Streamlit component) I haven’t found any way to style elements based on the current browser theme.
I’ve tried:
Accessing Streamlit config at runtime (this only stores manually set configurations)
Using javascript (only possible in a Streamlit component, which is way too much overhead)
Introspecting into Streamlit (it’s complicated, wasn’t able to find where this is stored, might not be possible)
+1 would help a lot when styling charts as default styling inverts color in dark vs light mode.
EDIT: If one is comfortable using streamlit_javascript then this code will return the user’s system preference on load. So if light/dark mode is forced in streamlit it will still return system preference and neither will toggling it in system settings adjust the value dynamically but only on browser load, ie a browser refresh must be forced. Just tried on Mac and will try on Windows tomorrow.
@PZ1 thanks for the suggestion! I came up with a variation on your snippet where it would read the selected theme from CSS of streamlit div. It seem to work with whatever the use choose, but still unfortunately require a page refresh, because Streamlit does not trigger re-run when the settings are changed
from streamlit_javascript import st_javascript
st_theme = st_javascript("""window.getComputedStyle(window.parent.document.getElementsByClassName("stApp")[0]).getPropertyValue("color-scheme")""")
if st_theme == "dark":
...
else:
...
Offtopic: This is how I use it with a canvas widget
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.