I’m encountering an issue with coloring buttons in Streamlit and seeking guidance on how to achieve this. I’m attempting to create two buttons with different background colors, one green and the other red, using Streamlit’s st.button.
Here’s the code snippet I’ve tried:
# Define custom CSS for button colors
button1_color = "#00FF00" # Green color for Button 1
button2_color = "#FF0000" # Red color for Button 2
# Create buttons with st.button
button1_clicked = st.button("Button 1", key="button1")
button2_clicked = st.button("Button 2", key="button2")
# Check button states and print messages
if st.button("Submit"):
if button1_clicked:
st.write("Button 1 pressed")
elif button2_clicked:
st.write("Button 2 pressed")
# Apply CSS to modify button appearance
button_style = f"""
<style>
div[data-baseweb="button"] div:nth-child(1) button {{
background-color: {button1_color} !important;
color: white !important;
}}
div[data-baseweb="button"] div:nth-child(2) button {{
background-color: {button2_color} !important;
color: white !important;
}}
</style>
"""
st.markdown(button_style, unsafe_allow_html=True)
However, the button colors don’t seem to change as intended. I’ve attempted to use CSS styling to modify the buttons’ appearance, but it doesn’t reflect the specified colors.
Is there a better approach or a different way to set button colors in Streamlit? Any insights or guidance would be greatly appreciated.
Your form button should be in a form and you’ll still need to refer to the HTML element “button.” The stylable container can’t natively identify Streamlit elements by their (Streamlit function) names. The CS needs to be targeted to the underlying HTML (which you can get to by inspecting elements.)
Try this:
import streamlit as st
from streamlit_extras.stylable_container import stylable_container
# Create buttons with st.button
with stylable_container(
"green",
css_styles="""
button {
background-color: #2E8B57;
color: #F5F5F5
}""",
):
f = st.form("my_form")
if f.form_submit_button(label="Place"):
st.write("Place pressed")
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.