You will find Plotly templates in light and dark mode for each of the 26 Bootstrap/Bootswatch themes available in the Dash Bootstrap Components Library.
As of 2.1.0, there are also figure templates based on the Vizro theme.
These templates will automatically style your figures with Bootstrap theme colors and fonts.
As of V1.1 a dark mode is available for each theme. This is ideal for use with the Bootstrap Color Modes available as of Bootstrap 5.3.0
Two All-in-One components to change themes in a Dash app.
ThemeSwitchAIO toggles between two themes. (For use with Bootstrap < 5.3 Otherwise, use Bootstrap Color Modes)
ThemeChangerAIO select from multiple themes.
Examples of a Color Mode Switch to toggle between a light and dark theme.
The dbc.css stylesheet which styles Dash AG Grid, Dash Core Components and the Dash DataTable with a Bootstrap theme.
Dash Bootstrap Templates V2.0.0 required Plotly >= 6.0 If your project requires Plotly 5, please use Dash Bootstrap Templates V1.3.0
The ThemeChangerAIO component and the dbc.css stylesheet requires Dash Bootstrap Components>=V1.0.0. It will only
work with the themes included in Dash Bootstrap Components>=V1.0.0.
As of V1.0.8, the themes in the ThemeSwitchAIO component can be specified as a pathname or a URL. This allows for working
off-line and with custom stylesheets.
The Bootstrap themed Plotly figure templates can be used with any Plotly figure. It does not require Dash or
the Dash Bootstrap Components library.
"""
A sample of 8 of the 26 Bootstrap themed Plotly figure templates available
in the dash-bootstrap-template library
"""
from dash import Dash, html, dcc
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
import plotly.express as px
df = px.data.gapminder()
templates = [
"bootstrap",
"minty",
"pulse",
"flatly",
"quartz",
"cyborg",
"darkly",
"vapor",
]
load_figure_template(templates)
figures = [
px.scatter(
df.query("year==2007"),
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
log_x=True,
size_max=60,
template=template,
title="Gapminder 2007: '%s' theme" % template,
)
for template in templates
]
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([dcc.Graph(figure=fig, className="m-4") for fig in figures])
if __name__ == "__main__":
app.run_server(debug=True)
dbc.css stylesheet
The dash-ag-grid, dash-core-components, the Dash DataTable and Plotly figures are not automatically styled with a Bootstrap theme.
An easy way to make your Dash components look better with a Bootstrap theme is to use the stylesheet from
the dash-bootstrap-templates library. This stylesheet defines the "dbc" class.
Adding className="dbc dbc-ag-grid" minimally styles Dash components with your selected Bootstrap theme:
Makes the text readable in both light and dark themes.
Uses theme's font-family.
Changes the accent color to the theme's primary color
You can add the dbc class as an external stylesheet like this:
That's it! Simply adding className="dbc dbc-ag-grid" will make Dash AG Grid, Dash Core Components and the DataTable look better with ALL themes included in the dash-bootstrap-components library.
dash-bootstrap-templates has two All-in-One components to change themes.
The ThemeSwitchAIO is a switch with icons on the left and right, which is ideal for toggling between a light and a dark theme.
The ThemeChangerAIO has a button that opens an dbc.Offcanvas component which by default shows all the available themes.
Note the All-in-One component switches the Bootstrap stylesheet for the app and sets the default Plotly figure template
for the theme, however, figures must be updated in a callback in order to render the figure with the new template.
See the callback below for an example. The template_from_url is a helper function that returns the template name
based on the theme url. For example template_from_ur(dbc.themes.SLATE) returns "slate"
Dash Labs is Plotly library that explores new features for future releases of Dash.
In Dash Labs V0.4.0, there was a cool feature where Bootstrap themed figure templates were created "on the fly". This was a
part of the layout templates project that is no longer being developed.
Even though these Bootstrap themed figure templates will not be included in Dash, the dash-bootstrap-templates makes
them available to you. The figure templates are created using the Dash Labs' algorithms and saved in json format. When
you use load_figure_template() in your app, it loads the json file, adds it to plotly.io and sets it as the default figure template for an app. See more
information about Plotly figure templates here.
Available Themes
This library provides a figure template for the following Bootstrap/Bootswatch themes:
Note in dark themes ["cyborg", "darkly", "slate", "solar", "superhero", "vapor"], there is not much difference in the figure templates in light or dark color modes.
ThemeChangerAIO Reference
ThemeChangerAIO is an All-in-One component composed of a parent html.Div with
the following components as children:
dbc.Button ("switch") Opens the Offcanvas component for user to select a theme.
dbc.Offcanvas ("offcanvas")
dbc.RadioItems ("radio"). The themes are displayed as RadioItems inside the dbc.Offcanvas component.
The value is a url for the theme
html.Div is used as the Output of the clientside callbacks.
The ThemeChangerAIO component updates the stylesheet when the value of radio changes. (ie the user selects a new theme)
param: radio_props A dictionary of properties passed into the dbc.RadioItems component. The default value is dbc.themes.BOOTSTRAP
param: button_props A dictionary of properties passed into the dbc.Button component.
param: offcanvas_props. A dictionary of properties passed into the dbc.Offcanvas component
param: aio_id The All-in-One component ID used to generate components' dictionary IDs.
The All-in-One component dictionary IDs are available as:
ThemeChangerAIO.ids.radio(aio_id)
ThemeChangerAIO.ids.offcanvas(aio_id)
ThemeChangerAIO.ids.button(aio_id)
ThemeSwitchAIO Reference
ThemeSwitchAIO is an All-in-One component composed of a parent html.Div with the following components as children:
dbc.Switch ("switch") with icons to the left and right of the switch.
dcc.Store ("store") The themes are stored in the data prop.
html.Div is used as the Output of the clientside callbacks.
The ThemeSwitchAIO component updates the stylesheet when triggered by changes to the value of switch or when
the themes are updated in the "store" component. The themes in the switch may be updated in a callback
by changing the theme urls in the "store" component.
param: switch_props A dictionary of properties passed into the dbc.Switch component.
param: themes A list of two urls for the external stylesheets or pathnames to files.
param: icons A dict of the icons to the left and right of the switch. The default is {"left" :"fa fa-moon", "right" :"fa fa-sun"}.
param: aio_id The All-in-One component ID used to generate component's dictionary IDs.
The All-in-One component dictionary IDs are available as
ThemeSwitchAIO.ids.switch(aio_id)
ThemeSwitchAIO.ids.store(aio_id)
Contributors
Special thanks to @tcbegley and @emilhe for their help with this project.
Works well with
Use together with dash-bootstrap-templates: extensions, drivers, servers and companion tools.
plotlyPythie’s Index: 86An open-source interactive data visualization library for Python62.4M downloads/mo
dashPythie’s Index: 82A Python framework for building reactive web-apps. Developed by Plotly.8.3M downloads/mo