Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Dash Style Design

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:


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:


2. External CSS File

By loading an external CSS file, you can set global styles for the entire application.

  1. Create a CSS file (such as assets/styles.css).

  2. 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:


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:


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: