I am trying my hand at creating a dashboard. I have a public airline data to work with. I need my dashboard to show a bar plot for the number of flights per month for an entered year and an entered airline. Somehow it worked with only one input for a year, but it does not work with two inputs. What am I doing wrong here? Thanks in advance.
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()```
When I run the dashboard, I get the following error:
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]
This means you have a typo in the component_id
argument of your second input and you can fix it by replacing 'input_air'
with 'input-air'
:
Input(component_id = 'input-air', component_property = 'value')