官网

Dash Documentation & User Guide | Plotly

开始使用

安装

pip install dash

我们还建议安装 Pandas,这是 Plotly Express 所要求的,并在我们的许多示例中使用。

pip install pandas

最小的Dash例子

from dash import Dash, html, dcc, callback, Output, Input
import plotly.express as px
import pandas as pd

df = pd.read_csv('<https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv>')

app = Dash(__name__)

app.layout = html.Div([
    html.H1(children='Title of Dash App', style={'textAlign':'center'}),
    dcc.Dropdown(df.country.unique(), 'Canada', id='dropdown-selection'),
    dcc.Graph(id='graph-content')
])

@callback(
    Output('graph-content', 'figure'),
    Input('dropdown-selection', 'value')
)
def update_graph(value):
    dff = df[df.country==value]
    return px.line(dff, x='year', y='pop')

if __name__ == '__main__':
    app.run(debug=True)

运行python app.py 即可

Untitled

快速开始

Hello world

from dash import Dash, html

app = Dash(__name__)

app.layout = html.Div([
    html.Div(children='Hello World')
])

if __name__ == '__main__':
    app.run(debug=True)

从返回的源代码可以看到,框架已经帮我们预设了很多的样式,而不是简单地返回一行文字

Untitled

Untitled

详细请看

Dash in 20 Minutes Tutorial | Dash for Python Documentation | Plotly