Python 中的 Dash 简介
Dash 是一个成功的 Python 框架,已成为创建数据可视化界面的热门选择。它是用纯 Python 构建具有高度交互性用户界面的分析性 Web 应用程序的理想选择。本文详细介绍了 Dash,并提供了有用的示例来帮助您入门。
什么是 Dash?
Dash 是由 Plotly 创建的 Python 框架,它允许您创建分析性在线应用程序,而无需了解 JavaScript、CSS 或 HTML。 Dash 应用有两个主要组件:布局(指定应用的显示方式)和交互(指定应用的运行方式)。
开始使用 Dash
Pip 是 Python 的软件包安装程序,可用于安装 Dash −
pip install dash
安装 Dash 软件包后,您可以在 Python 脚本中导入它 −
import dash import dash_core_components as dcc import dash_html_components as html
构建 Dash 应用
现在让我们看看如何创建 Dash应用程序。
示例 1:基本 Dash 应用程序
此处提供了一个非常简单的 Dash 应用程序的代码 −
import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash(__name__) app.layout = html.Div(children=[ html.H1(children='Hello, Dash!'), html.Div(children=''' Dash: A web application framework for Python. '''), dcc.Graph( id='example-graph', figure={ 'data': [ {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'}, {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montreal'}, ], 'layout': { 'title': 'Dash Data Visualization' } } ) ]) if __name__ == '__main__': app.run_server(debug=True)
程序中显示标题、文本和直观的条形图。
示例 2:使用 Dash 回调实现交互
Dash 通过使用回调使应用程序具有交互性。回调链接各个部分,这样当输入发生变化时,输出也会发生变化。以下是一个例子 −
import dash from dash.dependencies import Input, Output import dash_core_components as dcc import dash_html_components as html app = dash.Dash(__name__) app.layout = html.Div([ dcc.Input(id='my-input', value='initial value', type='text'), html.Div(id='my-output') ]) @app.callback( Output(component_id='my-output', component_property='children'), [Input(component_id='my-input', component_property='value')] ) def update_output_div(input_value): return 'You've entered "{}"'.format(input_value) if __name__ == '__main__': app.run_server(debug=True)
在此示例中,如果"my-input"组件发生变化,回调将更新"my-output"组件。
示例 3:多个输入和输出
Dash 支持具有多个输入和输出的回调−
import dash from dash.dependencies import Input, Output import dash_core_components as dcc import dash_html_components as html app = dash.Dash(__name__) app.layout = html.Div([ dcc.Input(id='input-1', type='text'), dcc.Input(id='input-2', type='text'), html.Div(id='output') ]) @app.callback( Output(component_id='output', component_property='children'), [Input(component_id='input-1', component_property='value'), Input(component_id='input-2', component_property='value')] ) def update_output_div(input1, input2): return 'You've entered "{}" and "{}"'.format(input1, input2) if __name__ == '__main__': app.run_server(debug=True)
在此图中,两个输入组件连接到回调函数 update_output_div。输出组件会更新以反映任一输入值的任何更改。
示例 4:使用 Dash 核心组件
下拉列表、图表、markdown 块等高级组件可通过 Dash 提供的组件库获得,包括 dash_core_components 和 dash_html_components。以下是如何使用下拉列表组件的示例−
import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash(__name__) app.layout = html.Div([ dcc.Dropdown( id='dropdown', options=[ {'label': 'Option 1', 'value': '1'}, {'label': 'Option 2', 'value': '2'}, {'label': 'Option 3', 'value': '3'} ], value='1' ), html.Div(id='display-selected-values') ]) @app.callback( dash.dependencies.Output('display-selected-values', 'children'), [dash.dependencies.Input('dropdown', 'value')]) def set_display_children(selected_value): return 'You've selected "{}"'.format(selected_value) if __name__ == '__main__': app.run_server(debug=True)
在此图中,输出通过回调函数更新,以显示下拉菜单中的选定项。
结论
Python 框架 Dash 提供了一个用于构建基于 Web 的数据可视化应用程序的有效工具包。它的多功能性使用户能够仅使用 Python 代码构建复杂且交互式的仪表板,使其成为数据科学领域备受追捧的工具。
本文介绍了 Dash 及其功能,包括如何安装它以及如何将其与 Python 编程一起使用。此外,我们还研究了四个示例,每个示例都展示了如何以不同的方式开发 Dash 应用程序,包括如何设计一个简单的程序、添加与回调的交互、使用多个输入和输出以及利用 Dash 的基本组件。
然而,这里提供的示例只是暗示了 Dash 的全部潜力。为了进一步理解 Dash 的潜力,我们鼓励您进行更多调查并尝试各种组件和回调结构。对于复杂的应用程序,其官方文档是提供大量信息的重要资源。