Dash Style Design
When developing interactive web applications, Dash not only lets you use Python to build complex applications, but also provides rich styling options to make your application look more professional and beautiful.
In Dash, styling is implemented through CSS. Dash provides several ways to set component styles, including:
-
Inline Styles: Through
styleproperty to set the component's style directly. -
External CSS File: set global styles by loading an external CSS file.
-
Dash Bootstrap Components: use Bootstrap-style components and styles.
-
Dash DAQ: style components used to create data acquisition and control applications.
1. Inline Styles
Through the style property you can directly set CSS styles for a component. style is a dictionary where the keys are CSS properties and the values are CSS values.
Example
from
dash
import
Dash
,
html
Create the Dash App
app
=
Dash
(
name
)
Define the Layout
app.
layout
=
html.
Div
(
style
=
{
'backgroundColor'
:
'lightblue'
,
Background Color
'padding'
:
'20px'
,
Padding
'borderRadius'
:
'10px'
,
Rounded Corners
'textAlign'
:
'center'
Text Centering
}
,
children
=
[
html.
H1
(
"Welcome to Dash"
,
style
=
{
'color'
:
'darkblue'
}
)
,
html.
P
(
"This is a styled Div."
)
]
)
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Runtime Effect:
-
The background color is light blue, the padding is 20px, the border radius is 10px, and the text is centered.
-
The title text color is dark blue.
2. External CSS File
By loading an external CSS file, you can set global styles for the entire application.
-
Create a CSS file (such as
assets/styles.css). -
Load the CSS file in the Dash application.
CSS File Example (assets/styles.css):
Example: assets/styles.css file
/* Set global font */
body
{
font-family
:
Arial
,
sans-serif
;
}
/* Set heading style */
h1
{
color
:
darkblue
;
text-align
:
center
;
}
/* Set Div style */
.custom-div
{
background-color
:
lightblue
;
padding
:
20px
;
border-radius
:
10px
;
text-align
:
center
;
}
Dash uses the css file:
Example
from
dash
import
Dash
,
html
Create the Dash App
app
=
Dash
(
name
)
Define the Layout
app.
layout
=
html.
Div
(
className
=
'custom-div'
,
Use CSS class names
children
=
[
html.
H1
(
"Welcome to Dash"
)
,
html.
P
(
"This is a styled Div."
)
]
)
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Runtime Effect:
-
The application loads
assets/styles.cssthe styles in the file. -
h1The title color is dark blue, displayed centered. -
DivThe background color is light blue, with padding and rounded corners.
3. Dash Bootstrap Components
Dash Bootstrap Components (dash-bootstrap-components) is a third-party library that provides Bootstrap-style components and styles. It helps you quickly build beautiful Dash applications.
pip install dash-bootstrap-components
Example
from
dash
import
Dash
,
html
import
dash_bootstrap_components
as
dbc
Create the Dash App
app
=
Dash
(
name
,
external_stylesheets
=
[
dbc.
themes
.
BOOTSTRAP
]
)
Define the Layout
app.
layout
=
dbc.
Container
(
children
=
[
dbc.
Row
(
dbc.
Col
(
html.
H1
(
"Welcome to Dash Bootstrap"
,
className
=
"text-center text-primary"
)
)
)
,
dbc.
Row
(
dbc.
Col
(
dbc.
Card
(
children
=
[
dbc.
CardHeader
(
"Card Title"
)
,
dbc.
CardBody
(
children
=
[
html.
P
(
"This is a Bootstrap-style card."
,
className
=
"card-text"
)
]
)
]
,
className
=
"mt-4"
Set margin
)
)
)
]
)
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Runtime Effect:
-
Use Bootstrap theme styles.
-
The title is displayed centered, with a theme-blue color.
-
The card component has a title and content, with an attractive style.
4. Dash DAQ
Dash DAQ is a component library for creating data acquisition and control applications, providing rich styled components.
pip install dash-daq
Example
from
dash
import
Dash
,
html
import
dash_daq
as
daq
Create the Dash App
app
=
Dash
(
name
)
Define the Layout
app.
layout
=
html.
Div
(
style
=
{
'textAlign'
:
'center'
}
,
children
=
[
daq.
Thermometer
(
id
=
'thermometer'
,
value
=
25
,
min
=
0
,
max
=
100
,
label
=
"Thermometer"
,
style
=
{
'margin'
:
'20px'
}
)
,
daq.
Gauge
(
id
=
'gauge'
,
value
=
50
,
min
=
0
,
max
=
100
,
label
=
"Pressure Gauge"
,
style
=
{
'margin'
:
'20px'
}
)
]
)
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Runtime Effect:
-
Display a thermometer and a pressure gauge, with an attractive style.
-
The component is displayed centered, with margin.