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

Dash Core Components

Dash Core Components

Dash's core components are the foundation for building applications; they allow you to create user interfaces and interact with data.

Dash, through the dash.html and dash.dcc module, lets you easily create user interfaces and interact with data.

Starting from Dash 2.0, dash_html_components and dash_core_components have been integrated into the main dash package.

It is now recommended to import html and dcc directly from dash, rather than using the old dash_html_components and dash_core_components.


Overview of Dash Core Components

Dash's core components can be divided into two major categories:dash.html and dash.dcc.dash.html provides Python wrappers for HTML tags, while dash.dcc provides more advanced interactive components such as sliders, dropdown menus, and graphs.

1. dash.html Component

dash.html The module provides Python classes corresponding to HTML tags. These classes allow you to write HTML code in a Pythonic way. For example,dash.html.Div corresponds to the HTML

tag,dash.html.H1 Corresponds to

tag.

Example

import

dash

import

dash.

html

as

html

app

=

dash.

Dash

(

name

)

app.

layout

=

html.

Div

(

[

    html.

H1

(

'Welcome to the Dash World'

)

,

    html.

P

(

'This is a simple paragraph.'

)

,

    html.

Div

(

[

        html.

Span

(

'This is Span 1'

)

,

        html.

Span

(

'This is Span 2'

)

   

]

)

]

)

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

dash.html components make writing HTML structures very simple and intuitive.

In the code above, we created a structure containing a title, a paragraph, and two Span elements' Div container.

2. dash.dcc Component

dash.dcc module provides more advanced interactive components, which are typically used for data visualization and user input. Common dash.dcc components include Graph, Dropdown, Slider , etc.

Example Code

Example

import

dash

import

dash.

html

as

html

import

dash.

dcc

as

dcc

app

=

dash.

Dash

(

name

)

app.

layout

=

html.

Div

(

[

    dcc.

Graph

(

       

id

=

'example-graph'

,

        figure

=

{

           

'data'

:

[

               

{

'x'

:

[

1

,

2

,

3

]

,

'y'

:

[

4

,

1

,

2

]

,

'type'

:

'bar'

,

'name'

:

'RUNOOB'

}

,

               

{

'x'

:

[

1

,

2

,

3

]

,

'y'

:

[

2

,

4

,

5

]

,

'type'

:

'bar'

,

'name'

:

'GOOGLE'

}

,

           

]

,

           

'layout'

:

{

               

'title'

:

'Dash Data Visualization'

           

}

       

}

   

)

,

    dcc.

Dropdown

(

       

id

=

'example-dropdown'

,

        options

=

[

           

{

'label'

:

'Option 1'

,

'value'

:

'1'

}

,

           

{

'label'

:

'Option 2'

,

'value'

:

'2'

}

,

           

{

'label'

:

'Option 3'

,

'value'

:

'3'

}

       

]

,

        value

=

'1'

   

)

]

)

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

In this example, we created a layout containing a bar chart and a dropdown menu.dcc.Graph component is used to display data visualization charts, while the dcc.Dropdown component allows the user to select options from a dropdown menu.

Properties of Core Components

Every Dash component has some properties that control its appearance and behavior. For example,dash.html.Div 's children property is used to specify its child elements,dash.dcc.Graph 's figure property is used to specify the chart's data and layout.

Common Properties

  • children: used to specify the component's child elements. Most components have this property.
  • id: used to uniquely identify the component, usually used in callback functions.
  • style: used to set the component's CSS style.
  • className: used to specify the component's CSS class name.

Example Code

Example

import

dash

import

dash.

html

as

html

app

=

dash.

Dash

(

name

)

app.

layout

=

html.

Div

(

    children

=

[

        html.

H1

(

'This is a title'

,

style

=

{

'color'

:

'blue'

,

'fontSize'

:

40

}

)

,

        html.

P

(

'This is a paragraph'

,

className

=

'my-class'

)

   

]

,

    style

=

{

'backgroundColor'

:

'#f0f0f0'

}

)

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

In the code above, we use style property to set the title's color and font size, and use the className property to assign a CSS class name to the paragraph.