I am just trying to make something more simple for the users, where they can easily switch between two themes by a single click.
Maybe using this feature : https://extras.streamlit.app/Toggle%20Switch
?
Ok, you can use CSS classes to change properties from the Streamlit UI, it’s not officially supported and can break with future changes and updates, that’s why the theme option and the config.toml are the best way to do this.
To know the CSS classes that Streamlit uses, you need to use the browser dev tools and inspect the elements.
Here is an example using the main .stApp
import streamlit as st
dark = '''
<style>
.stApp {
background-color: black;
}
</style>
'''
light = '''
<style>
.stApp {
background-color: white;
}
</style>
st.markdown(light, unsafe_allow_html=True)
# Create a toggle button
toggle = st.button("Toggle theme")
# Use a global variable to store the current theme
if "theme" not in st.session_state:
st.session_state.theme = "light"
# Change the theme based on the button state
if toggle:
if st.session_state.theme == "light":
st.session_state.theme = "dark"
else:
st.session_state.theme = "light"
# Apply the theme to the app
if st.session_state.theme == "dark":
st.markdown(dark, unsafe_allow_html=True)
else:
st.markdown(light, unsafe_allow_html=True)
# Display some text
st.write("This is a streamlit app with a toggle button for themes.")
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.