2

This question seems to simple but I don't seem to find a proper solution. I have a Dash app which is styled with bootstrap like this

from dash import Dash
import dash_bootstrap_components as dbc

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([dbc.Button("Test"),
                            dbc.RadioItems(["Test1", "Test2"], "Test1")])

if __name__ == '__main__':
    app.run_server(debug=True)

Which gives enter image description here

But I'd like to change the primary blue color to something else. I'm sure there must be a way to simply change this for all possible components at once but I can't find how. I've found ways to do this by changing the css of each component but not for all components at once. This question comes somewhat close but doesn't really solve the issue and has the additional constraint that no additional css file can be used.

debsim
  • 582
  • 4
  • 19
  • 1
    @JohnCollins I'm using dbc.RadioItems in my app now and it's working as I expect, it's also documenten in the dash bootstrap components documentation here: https://dash-bootstrap-components.opensource.faculty.ai/docs/components/input/ – debsim Aug 24 '23 at 09:52
  • 1
    Ah! You're absolutely right - I missed that (obviously). Thanks for sharing the link to docs; I did find it strange they wouldn't cover radio buttons. I was getting an error with my version of installed `dbc` (that `dbc.RadioItems` didn't exist), but I must have not the latest version (or something). – John Collins Aug 24 '23 at 14:06

1 Answers1

1

For DBC components, you could try using the CSS wildcard selector [class*="primary"]

Note: Radio items are a sort of exception when it comes to components and have to be handled specially (see CSS below).

Create a file named custom.css and store it in a directory named "assets" located locally to the main dash app.py file, like so:

.
├── app.py
└── assets
    └── custom.css

2 directories, 2 files

where, for example, the app.py file could contain:

import dash_bootstrap_components as dbc

from dash import Dash, dcc, html


app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

badge = dbc.Button(
    ["Notifications", dbc.Badge("4", color="primary")], color="primary",
)
dropdown = dbc.DropdownMenu(
    label="Menu",
    children=[
        dbc.DropdownMenuItem("Item 1"),
        dbc.DropdownMenuItem("Item 2"),
        dbc.DropdownMenuItem("Item 3"),
    ],
    color="primary",
)

app.layout = dbc.Container(
    [
        html.H2("Dash App"),
        html.H3("Change Primary Color of Components"),
        dbc.Button("Test", color="primary"),
        dcc.RadioItems(["Test1", "Test2"], id="Test1"),
        badge,
        dbc.Alert("This is a primary alert", color="primary"),
        dbc.Spinner(color="primary"),
        dropdown,
    ],
    style={"textAlign": "center"},
    id="container",
)

if __name__ == "__main__":
    app.run_server(debug=True)

and the custom.css file contain:

[class*="primary"], input[type='radio'] {
    color: #fff;
    background-color: red!important;
    accent-color: red;
    border-color: orange!important;
    margin: 0.2rem;
}
[class*="primary"]:hover {
    background-color: maroon!important;
}
.spinner-border {
    border-right-color: transparent!important;
}

which will result in:

GIF screen recording showing near total global override of primary components' color from blue to red

John Collins
  • 2,067
  • 9
  • 17
  • This does indeed change the color of the button but it keeps the primary color of the radio button (or any other component) to the default bootstrap blue. I'd like to change all these colors at once. – debsim Aug 23 '23 at 10:54
  • 1
    Radio buttons are a bit tricky (also see my comment on your question above); but I have updated my answer demonstrating a CSS-based approach for globally overriding the default dbc [and specifically radio input] components' `*primary*` color. Also, keep in mind, the default primary colors (which are not merely one color but a small variety of shades of blue - e.g., outlines are a different shade) themselves are not set by one global setting - but a variety of CSS scattered throughout various files. – John Collins Aug 24 '23 at 08:36
  • Thanks a lot for the updated answer! The only component that's still acting weird is the dcc.Loading but after some looking around this seems to be not so straightforward. – debsim Aug 24 '23 at 09:51