Dash Web-app doesn't work. Error: POST /_dash-update-component HTTP/1.1" 500 -

Dash Web-app doesn't work. Error: POST /_dash-update-component HTTP/1.1" 500 -

我刚刚开始学习 Python 网络应用程序库“Dash”,这就是为什么我可能会问一些愚蠢的问题。

基本上我的以下代码将使用 python 库“Dash”创建一个仪表板。如果一切正常,我可以 select 从 3 个股票代码(苹果、特斯拉、可口可乐)的下拉列表中选择一个。 select点击后,将显示 2016 年 1 月 1 日至今的股价。

我的代码是:

from pandas_datareader import data
import plotly.graph_objects as go

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from datetime import datetime as dt

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H3('Stock Tickers'),
    
    dcc.Dropdown(
        id='my-dropdown',
        options=[
            {'label': 'Coke', 'value': 'COKE'},
            {'label': 'Tesla', 'value': 'TSLA'},
            {'label': 'Apple', 'value': 'AAPL'}
            ],
        value='COKE'
    ),
    
    dcc.Graph(id='my-graph', figure={})
])

@app.callback(
    [Output(component_id='my-graph', component_property='figure')],
    [Input(component_id='my-dropdown', component_property='value')])
def update_graph(dropdown_properties):
    selected_value = dropdown_properties['value']
    
    df = data.DataReader(selected_value, 'yahoo', dt(2016, 1, 1), dt.now())
    print(df[:5])
    
    figure = go.Figure(data=[go.Scatter(x=df.index, y=df.Close, 
                                     name=selected_value)])
    
    return {
        'figure': figure}

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

我收到以下错误消息:

Dash is running on http://127.0.0.1:8050/
    
     * Serving Flask app "Stock_Ticker" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
    127.0.0.1 - - [27/Mar/2021 13:20:10] "GET / HTTP/1.1" 200 -
    127.0.0.1 - - [27/Mar/2021 13:20:12] "GET /_dash-layout HTTP/1.1" 200 -
    127.0.0.1 - - [27/Mar/2021 13:20:12] "GET /_dash-dependencies HTTP/1.1" 200 -
    [2021-03-27 13:20:12,391] ERROR in app: Exception on /_dash-update-component [POST]
    Traceback (most recent call last):
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
        response = self.full_dispatch_request()
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
        raise value
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
        rv = self.dispatch_request()
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\dash\dash.py", line 1078, in dispatch
        response.set_data(func(*args, outputs_list=outputs_list))
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\dash\dash.py", line 1009, in add_context
        output_value = func(*args, **kwargs)  # %% callback invoked %%
      File "C:\Users\Gunardilin\Desktop\Value Investing Dashboard\Dash\Stock_Ticker.py", line 39, in update_graph
        selected_value = dropdown_properties['value']
    TypeError: string indices must be integers
    127.0.0.1 - - [27/Mar/2021 13:20:12] "POST /_dash-update-component HTTP/1.1" 500 -
    [2021-03-27 13:20:18,959] ERROR in app: Exception on /_dash-update-component [POST]
    Traceback (most recent call last):
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
        response = self.full_dispatch_request()
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
        raise value
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
        rv = self.dispatch_request()
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\dash\dash.py", line 1078, in dispatch
        response.set_data(func(*args, outputs_list=outputs_list))
      File "C:\Users\Gunardilin\anaconda3\lib\site-packages\dash\dash.py", line 1009, in add_context
        output_value = func(*args, **kwargs)  # %% callback invoked %%
      File "C:\Users\Gunardilin\Desktop\Value Investing Dashboard\Dash\Stock_Ticker.py", line 39, in update_graph
        selected_value = dropdown_properties['value']
    TypeError: string indices must be integers
    127.0.0.1 - - [27/Mar/2021 13:20:18] "POST /_dash-update-component HTTP/1.1" 500 -

从 link (http:...),我只能看到一个空白图表:

如果有人能指出我的错误,我将不胜感激。

下拉列表中的值是以下之一:

'COKE'
'TSLA'
'AAPL'

但你把它当作是这样处理的:

{'label': 'Coke', 'value': 'COKE'},
{'label': 'Tesla', 'value': 'TSLA'},
{'label': 'Apple', 'value': 'AAPL'}

基本上,你可以删除这一行:

selected_value = dropdown_properties['value'] and your code should work.