添加第二个用户输入后,仪表板停止工作
Dashboard stopped working after I added a second user input
我正在尝试创建仪表板。我有一个 public 航空公司数据要处理。我需要我的仪表板显示输入年份和输入航空公司每月航班数量的条形图。不知何故,它只使用一个输入一年,但它不适用于两个输入。我在这里做错了什么?提前致谢。
import pandas as pd
import plotly.express as px
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input,Output
# Read the airline data into pandas dataframe
airline_data = pd.read_csv('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/airline_data.csv',
encoding = "ISO-8859-1",
dtype={'Div1Airport': str, 'Div1TailNum': str,
'Div2Airport': str, 'Div2TailNum': str})
app = dash.Dash(__name__)
# Set up the dashboard
app.layout = html.Div(children=[html.H1('Airline Dashboard',style={'textAlign': 'center', 'color': '#503D36', 'font-size': 40}),
html.Div(["Input Year: ", dcc.Input(id = 'input-yr', value = '2005', type = 'number',
style = {'height':'50px','font-size':35})],style = {'font-size':40}),
html.Div(["Input Airline: ", dcc.Input(id = 'input-air', value = 'AS', type = 'text',
style = {'height':'50px','font-size':35})],style = {'font-size':40}),html.Br(),
html.Br(),
html.Div(dcc.Graph(id = 'bar-plot'))])
@app.callback(Output(component_id = 'bar-plot', component_property = 'figure'),
[Input(component_id = 'input-yr', component_property = 'value'),
Input(component_id = 'input_air', component_property = 'value')])
def get_graph(entered_year,entered_airline):
df = airline_data[(airline_data['Year']==int(entered_year))&(airline_data['Reporting_Airline']==entered_airline)]
g1 = df.groupby(['Month'])['Flights'].sum().reset_index()
fig1 = px.bar(g1,x='Month',y='Flights',title='Number of flights per month in '+str(entered_year)+' for '+str(entered_airline)+' airline')
return fig1
# Run the dashboard
if __name__ == '__main__':
app.run_server()```
当我 运行 仪表板时,出现以下错误:
A nonexistent object was used in an `Input` of a Dash callback.
The id of this object is `input_air` and the property is `value`.
The string ids in the current layout are: [input-yr, input-air, bar-plot]
这意味着您在第二个输入的 component_id
参数中有错字,您可以通过将 'input_air'
替换为 'input-air'
:
来修复它
Input(component_id = 'input-air', component_property = 'value')
我正在尝试创建仪表板。我有一个 public 航空公司数据要处理。我需要我的仪表板显示输入年份和输入航空公司每月航班数量的条形图。不知何故,它只使用一个输入一年,但它不适用于两个输入。我在这里做错了什么?提前致谢。
import pandas as pd
import plotly.express as px
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input,Output
# Read the airline data into pandas dataframe
airline_data = pd.read_csv('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/airline_data.csv',
encoding = "ISO-8859-1",
dtype={'Div1Airport': str, 'Div1TailNum': str,
'Div2Airport': str, 'Div2TailNum': str})
app = dash.Dash(__name__)
# Set up the dashboard
app.layout = html.Div(children=[html.H1('Airline Dashboard',style={'textAlign': 'center', 'color': '#503D36', 'font-size': 40}),
html.Div(["Input Year: ", dcc.Input(id = 'input-yr', value = '2005', type = 'number',
style = {'height':'50px','font-size':35})],style = {'font-size':40}),
html.Div(["Input Airline: ", dcc.Input(id = 'input-air', value = 'AS', type = 'text',
style = {'height':'50px','font-size':35})],style = {'font-size':40}),html.Br(),
html.Br(),
html.Div(dcc.Graph(id = 'bar-plot'))])
@app.callback(Output(component_id = 'bar-plot', component_property = 'figure'),
[Input(component_id = 'input-yr', component_property = 'value'),
Input(component_id = 'input_air', component_property = 'value')])
def get_graph(entered_year,entered_airline):
df = airline_data[(airline_data['Year']==int(entered_year))&(airline_data['Reporting_Airline']==entered_airline)]
g1 = df.groupby(['Month'])['Flights'].sum().reset_index()
fig1 = px.bar(g1,x='Month',y='Flights',title='Number of flights per month in '+str(entered_year)+' for '+str(entered_airline)+' airline')
return fig1
# Run the dashboard
if __name__ == '__main__':
app.run_server()```
当我 运行 仪表板时,出现以下错误:
A nonexistent object was used in an `Input` of a Dash callback.
The id of this object is `input_air` and the property is `value`.
The string ids in the current layout are: [input-yr, input-air, bar-plot]
这意味着您在第二个输入的 component_id
参数中有错字,您可以通过将 'input_air'
替换为 'input-air'
:
Input(component_id = 'input-air', component_property = 'value')