environment
python 3.8
dash 2.9.2
foreword
dash
It is a library for creating web
applications python
. It is built on Plotly.js
(developed by the same team), React
and Flask
above . The main user groups are data analysts and AI
practitioners. It can help them quickly build very beautiful web applications without you understand javascript
.
Install
pip
Install using the command
pip install dash
example
Let's look at the simplest example first
from dash import html, Dash
app = Dash(__name__)
app.layout = html.Div(
[
html.H1('第一级标题'),
html.H2('第二级标题'),
html.P(['这是一个段落', html.Br(), '换行后的段落']),
html.Table(
html.Tr(
[
html.Td('第一列'),
html.Td('第二列')
]
)
)
]
)
if __name__ == '__main__':
app.run_server(debug=True)
Take a closer look, the structure of the code is very similar to flask
that of , app.run_server
which is to start a web
service , and app
the object of layout
is the page content, in the example, titles, paragraphs and tables
Execute the above code, and then open the page in the browserhttp://127.0.0.1:8050
Callback
Dash
The core of the application is the callback function. A callback function is a way of responding to user interaction, such as clicking a button or selecting a drop-down list. We can use callback functions to update component content or perform some action on the page.
Dash
In , callback functions app.callback
are defined using the decorator. The input of the callback function is one or more variables, and the output is one or more variables. Here is a simple example
from dash import dcc, html, Dash
from dash.dependencies import Input, Output
app = Dash(__name__)
app.layout = html.Div([
dcc.Input(id='input', value='Dash', type='text'),
html.Div(id='output')
])
@app.callback(
Output(component_id='output', component_property='children'),
[Input(component_id='input', component_property='value')]
)
def update_output_div(input_value):
return f'您输入的是:{input_value}'
if __name__ == '__main__':
app.run_server(debug=True)
In this example, we define a layout that contains an input box and an output box. Then, we use app.callback
the decorator to define a callback function that takes the value of the input box as a parameter and returns a string. The output of the callback function is associated with Output
the component , i.e. output to output
the component's children
property .
component interaction
In addition to callback functions, Dash
many interactive components are provided, such as drop-down lists, slider bars, and date pickers. We can use these components to allow users to interact with our application. Here is a simple example
from dash import dcc, html, Dash
from dash.dependencies import Input, Output
app = Dash(__name__)
# 定义下拉选项
dropdown_options = [{'label': 'Blue', 'value': 'blue'},
{'label': 'Red', 'value': 'red'},
{'label': 'Yellow', 'value': 'yellow'},
{'label': 'Green', 'value': 'green'}]
# 定义app布局
app.layout = html.Div([
dcc.Dropdown(options=dropdown_options,
value='blue',
id='color-dropdown'),
html.Div('Hello, world!', id='output-div', style={'padding': '20px'})
], style={'backgroundColor': 'blue'}) # Set the initial background color
# Define the app callback
@app.callback(Output('output-div', 'style'), [Input('color-dropdown', 'value')])
def update_output_background_color(selected_color):
if selected_color:
# 更新div的背景色
return {'backgroundColor': selected_color}
if __name__ == '__main__':
app.run_server(debug=True)
In this example, we use dcc.Dropdown
the component to create a dropdown list and output
associate its selected value with the component's style. The callback function takes the value of the drop-down list as a parameter and returns a dictionary containing background-color
the property , which is used to set the background color of the output box.
Summarize
Dash
is a powerful Python
application framework for building interactive data visualization dashboards. It provides rich HTML
and interactive components, as well as easy-to-use callback functions, so that we can easily create beautiful applications. Through the introduction of this article, we can understand Dash
the basic structure and usage of and use code samples to demonstrate how to create a simple Dash
application.
Topics on Python Practical Modules
For more useful python
modules , please move to
https://xugaoxiang.com/category/python/modules/