繁体   English   中英

python web 应用程序前端为什么会抛出异常?

[英]Why front end of the python web application throws an exception?

我正在构建一个 python web 应用程序以在时间序列图中显示黄金价格变动。 但是我的回调不起作用,它在应用程序的前端抛出以下异常。

ID not found in layout
6:42:46 AM
Attempting to connect a callback Input item to component:
  "none"
but no components with that id exist in the layout.

If you are assigning callbacks to components that are
generated by other callbacks (and therefore not in the
initial layout), you can suppress this exception by setting
`suppress_callback_exceptions=True`.
This ID was used in the callback(s) for Output(s):
  this_year_graph.figure

代码:

import pandas as pd
import plotly.express as px
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

from urllib.request import urlopen, Request

url = "http://goldpricez.com/gold/history/lkr/years-3"

req = Request(url=url)
html_content = urlopen(req).read().decode('utf-8')
df = pd.read_html(url)  # this will give you a list of dataframes from html

df1 = df[3]

first_val = df1.iloc[0][0]

date = df1[0]
price = df1[1]
data = [df1[0],df1[1]]
headers = ["Date", "Price"]

df3 = pd.concat(data, axis=1, keys=headers)

df3['Date'] = df3['Date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y'))
df3['Year'] = pd.DatetimeIndex(df3['Date']).year
df3['Month'] = pd.DatetimeIndex(df3['Date']).month
df3['Day'] = pd.DatetimeIndex(df3['Date']).day
df3['WDay'] = df3['Date'].dt.dayofweek
df3['WeekDayName'] = pd.DatetimeIndex(df3['Date']).day_name()

print(df3['WDay'])

writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df3.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H1("Gold Price Analyst" , style={'text-align' : 'center'}),
    dcc.Graph(id='this_year_graph', figure={})
])

@app.callback(
    [Output(component_id='this_year_graph', component_property='figure')],
    [Input('none', 'children')]
)
def update_graph():
    dff = df3.copy()
    dff = [df4['Date'],df4['Price']]

    this_year_graph = px.line(dff , x=dff['Date'], y=dff['Price'])

    return fig

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

检查你的回调 function。 @app.callbackInput告诉应用程序哪个元素(按钮)应该运行这个回调。 Dash 尝试在您的案例中找到id="none"的 HTML 元素。

@app.callback(
    [Output(component_id='this_year_graph', component_property='figure')],
    [Input('none', 'children')]                       #### <<<<< HERE
)
def update_graph():
    dff = df3.copy()
    dff = [df4['Date'],df4['Price']]

    this_year_graph = px.line(dff , x=dff['Date'], y=dff['Price'])

    return fig

在您的布局中插入按钮或其他内容。 并将回调连接到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM