Plotly Dash回调错误更新输出图

时间:2019-10-18 18:53:09

标签: python-3.x plotly plotly-dash plotly-python

我正在尝试使用Plotly Dash创建交互式图表。该代码从用户读取符号名称,并从Yahoo Finance中提取历史数据,并使用滑块绘制烛台图。运行代码时,我在浏览器中收到此错误:

Callback error updating output-graph.children

enter image description here

源代码为:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas_datareader.data as web
import pandas as pd
from app import app
import datetime


app = dash.Dash()

app.layout = html.Div(children=[
        html.H1('Interactive Chart'),
        dcc.Input(id='input', value='', type='text'),
        html.Div(id='output-graph')
        ])

@app.callback(
        Output(component_id='output-graph', component_property = 'children'),
        [Input(component_id='input', component_property = 'value')])

def update_graph(input_data):

    start = datetime.datetime(2018, 6, 1)
    end = datetime.datetime.now()
    df = web.DataReader(input_data, 'yahoo', start, end)
    df['year'] = pd.DatetimeIndex(df.index).year
    df['date'] = pd.DatetimeIndex(df.index)

    return dcc.Graph(id='example-graph',figure ={'data':[go.Candlestick(x=df['date'],open=df['Open'],high=df['High'],low=df['Low'],close=df['Close'],
                                                                        increasing={'line': {'color': 'green'}},decreasing={'line': {'color': 'red'}})],
                                                'layout':{'title': str.upper(input_data), 
                                                          'height': 1000,
                                                          "spikedistance": 200,
                                                          "hoverdistance": 100,
                                                          "xaxis": {
                                                                  "showspikes": 'true',
                                                                  "spikemode": "across",
                                                                  "spikedash": "dash",
                                                                  "spikecolor": "#000000",
                                                                  "spikethickness": 1},
                                                           "yaxis": {
                                                                  "showspikes": 'true',
                                                                  "spikemode": 'across',
                                                                  "spikedash": "dash",
                                                                  "spikecolor": "#000000",
                                                                  "spikethickness": 1
                                                                  }}})


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

我不知道回调在哪里出错。

1 个答案:

答案 0 :(得分:1)

尝试一下:

return html.Div(dcc.Graph(id='example-graph', figure=dict(data=traces,layout=layout)))