Dash Webapp不工作。错误:POST/_DashUpdateComponentHTTP/1.1“500

2024-10-01 22:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

基本上,我下面的代码将使用python库“Dash”创建一个仪表板。如果一切正常,我可以从3个股票中选择一个(苹果、特斯拉、可口可乐)。选择后,将显示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 -

从链接(http:…)中,我只能看到一个空白图: enter image description here

如果有人能指出我的错误,我将非常感激


Tags: inpyappflaskvaluelibpackagesline
1条回答
网友
1楼 · 发布于 2024-10-01 22:31:54

下拉列表中的值显示为以下值之一:

'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.

相关问题 更多 >

    热门问题