Python破折号回调函数

时间:2019-06-12 20:31:01

标签: python plotly plotly-dash

这也许有很多问题,但是我很好奇,是否有人对结合这两个破折号脚本有任何建议。目的是合并下拉菜单以删除/添加可视化图上的数据点。

第一个脚本可以很好地可视化我的数据,第二个具有回调功能的脚本用于从plotly tutorials创建下拉菜单。

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)



df = pd.read_csv('boilerData.csv', index_col='Date', parse_dates=True)
df = df.fillna(method = 'ffill').fillna(method = 'bfill')




app.layout = html.Div([
    dcc.Graph(
        id='hws',
        figure={
            'data': [
                {'x': df.index, 'y': df.HWST, 'type': 'line', 'name': 'hwst'},
                {'x': df.index, 'y': df.HWRT, 'type': 'line', 'name': 'hwrt'},
                {'x': df.index, 'y': df.OAT, 'type': 'line', 'name': 'oat'},
            ],
            'layout': {
                'title': 'Heating System Data Visualization'
            }
        }
    )
])

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

下拉脚本:

import dash
import dash_html_components as html
import dash_core_components as dcc

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label': 'Outdoor Temp', 'value': 'OAT'},
            {'label': 'Hot Water Supply Temp', 'value': 'HWST'},
            {'label': 'Hot Water Return Temp', 'value': 'HWRT'}
        ],
        value=['OAT','HWST','HWRT'],
        multi=True
    ),
    html.Div(id='output-container')
])


@app.callback(
    dash.dependencies.Output('output-container', 'children'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_output(value):
    return 'You have selected "{}"'.format(value)


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

任何提示都可以帮助您,仍在学习...

1 个答案:

答案 0 :(得分:0)

您需要知道的是,回调从某个Dash元素(此处为下拉列表的Input)中提取value,并返回到Output,以获取另一个Dash元素的某些属性(这里是图中的figure;请注意,我们只更改了data属性)。

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
import numpy as np

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)



df = pd.read_csv('boilerData.csv', index_col='Date', parse_dates=True)
df = df.fillna(method = 'ffill').fillna(method = 'bfill')

app.layout = html.Div([

    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label': 'Outdoor Temp', 'value': 'OAT'},
            {'label': 'Hot Water Supply Temp', 'value': 'HWST'},
            {'label': 'Hot Water Return Temp', 'value': 'HWRT'}
        ],
        value=['OAT','HWST','HWRT'],
        multi=True
    ),

    dcc.Graph(
        id='hws',
        figure={
            'data': [
                {'x': df.index, 'y': df.HWST, 'type': 'line', 'name': 'hwst'},
                {'x': df.index, 'y': df.HWRT, 'type': 'line', 'name': 'hwrt'},
                {'x': df.index, 'y': df.OAT, 'type': 'line', 'name': 'oat'},
            ],
            'layout': {
                'title': 'Heating System Data Visualization'
            }
        }
    )
])


@app.callback(
    dash.dependencies.Output('hws', 'figure'),
    [dash.dependencies.Input('my-dropdown', 'value')])
def update_output(columns):
    return {"data": [{'x': df.index, 'y': df[col], 'type':'line', 'name': col}
                     for col in columns]}


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