No, it’s not possible to set the colours from the app.py file, just access them with the st.get_option function. To change the colour you can directly try out colour options in the config.toml file or you can use the more convenient (in my opinion) UI from the hamburger menu to dynamically try different colour options until you find one you like!
You can use the UI by going to the hamburger menu in the top right corner → settings and then clicking the edit active theme button!
the most challenging part is to identify the individual CSS selectors of buttons.
You may get them by “Inspect” and then copy “CSS selector” with a browser’s developer mode. You will get something like: .css-1qipfyj > div:nth-child(1) > button:nth-child(1)
this part 1qipfyj is random session to session, so you will need to use regex to replace this part with a match formula.
Also worth using the parent of the class as an additional separation tool.
Then, it will work reliably.
Just an observation and not a criticism of the solution because I use it here in my tests too!
One of the things I mentioned in another post is that this is not very cool to do, because if Streamlit decides to refactor your classes by renaming them, what will happen is a general crash in the users’ apps.
That’s why I think that Streamlit should always allow this to be done by a proprietary function, because that’s the only way it will guarantee a good operation.
Yes, you absolutely right. Even when new elements appear on the screen, the injected selector will stop working (as the path changes or the classes’ sequence is shifted).
So the better way would be adding a streamlit widget key as “a class” to element classes — in such case, using individual style becomes possible.
———
I would suggest adding a style property to the element arguments:
Your parent element (div) has no distinct attributes to catch it, then follow the child (button).
So, try the following to find the button class in your DOM:
how to change different buttons with different colors I tried with the same solution but its not working for me, when I inspect all the buttons are having same class name and when I am using that class name all buttons are changing into same color.
button kind=“secondary” class=“css-1x8cf1d edgvbvh10”>
data-testid=“stMarkdownContainer”
class=“css-1offfwp e16nr0p34”>
This is how my class looks like
btnfontcolor = '#000000' # default colour black
if btntxt= "Submit":
btnfontcolor = '#4E9F3D' # green
elif if btntxt= "Add":
btnfontcolor = '#FF0000' # red
# add as many conditions as you need
ChangeButtonColour(btntxt, btnfontcolor) # call the function as per the code mentioned earlier
I don’t know of any way that you can change colours based on the key, because
streamlit can target widgets via keys but it has lacking / straight-forward cosmetic options for widgets
the key is not exposed to JS (when you inspect) the page, so you cant lock on that specific widget for further processing.
Further, the class attached to the button may not be static across streamlit versions.
Do you want to change the button colour after the button is clicked? OR
Do you want it to change automatically basis the key when the application is run?
No worries at all. If it could be, I would like it be changed automatically when the code is running. I heard that by using the div>div path maybe I could locate that specific button, but I don’t actually know how to look at the div path…