Assignment
Assignment
Assignment
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Import required libraries\n",
"import pandas as pd\n",
"import dash\n",
"import dash_html_components as html\n",
"import dash_core_components as dcc\n",
"from dash.dependencies import Input, Output, State\n",
"from jupyter_dash import JupyterDash\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
"from dash import no_update"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Create a dash application\n",
"app = JupyterDash(__name__)\n",
"JupyterDash.infer_jupyter_proxy_config()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# REVIEW1: Clear the layout and do not display exception till callback
gets executed\n",
"app.config.suppress_callback_exceptions = True\n",
"\n",
"# Read the airline data into pandas dataframe\n",
"airline_data = pd.read_csv('https://cf-courses-data.s3.us.cloud-
object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-
SkillsNetwork/Data%20Files/airline_data.csv', \n",
" encoding = \"ISO-8859-1\",\n",
" dtype={'Div1Airport': str, 'Div1TailNum':
str, \n",
" 'Div2Airport': str, 'Div2TailNum':
str})\n",
"\n",
"\n",
"# List of years \n",
"year_list = [i for i in range(2005, 2021, 1)]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Compute graph data for creating yearly airline performance
report \n",
"\n",
"Function that takes airline data as input and create 5 dataframes
based on the grouping condition to be used for plottling charts and grphs.\
n",
"\n",
"Argument:\n",
" \n",
" df: Filtered dataframe\n",
" \n",
"Returns:\n",
" Dataframes to create graph. \n",
"\"\"\"\n",
"def compute_data_choice_1(df):\n",
" # Cancellation Category Count\n",
" bar_data = df.groupby(['Month','CancellationCode'])
['Flights'].sum().reset_index()\n",
" # Average flight time by reporting airline\n",
" line_data = df.groupby(['Month','Reporting_Airline'])
['AirTime'].mean().reset_index()\n",
" # Diverted Airport Landings\n",
" div_data = df[df['DivAirportLandings'] != 0.0]\n",
" # Source state count\n",
" map_data = df.groupby(['OriginState'])
['Flights'].sum().reset_index()\n",
" # Destination state count\n",
" tree_data = df.groupby(['DestState', 'Reporting_Airline'])
['Flights'].sum().reset_index()\n",
" return [bar_data, line_data, div_data, map_data, tree_data]\n",
"\n",
"\"\"\"Compute graph data for creating yearly airline delay report\n",
"\n",
"This function takes in airline data and selected year as an input and
performs computation for creating charts and plots.\n",
"\n",
"Arguments:\n",
" df: Input airline data.\n",
" \n",
"Returns:\n",
" Computed average dataframes for carrier delay, weather delay, NAS
delay, security delay, and late aircraft delay.\n",
"\"\"\"\n",
"def compute_data_choice_2(df):\n",
" # Compute delay averages\n",
" avg_car = df.groupby(['Month','Reporting_Airline'])
['CarrierDelay'].mean().reset_index()\n",
" avg_weather = df.groupby(['Month','Reporting_Airline'])
['WeatherDelay'].mean().reset_index()\n",
" avg_NAS = df.groupby(['Month','Reporting_Airline'])
['NASDelay'].mean().reset_index()\n",
" avg_sec = df.groupby(['Month','Reporting_Airline'])
['SecurityDelay'].mean().reset_index()\n",
" avg_late = df.groupby(['Month','Reporting_Airline'])
['LateAircraftDelay'].mean().reset_index()\n",
" return [avg_car, avg_weather, avg_NAS, avg_sec, avg_late]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Application layout\n",
"app.layout = (html.Div(children=[ \n",
" # TODO1: Add title to the dashboard\
n",
" html.H1('US Domestic Airline Flights
Performance',\n",
" style={'text-align-
last':'centre','color':'#503D36','font-size':24}\n",
" ),\n",
" # REVIEW2: Dropdown creation\n",
" # Create an outer division \n",
" html.Div([\n",
" # Add an division\n",
" html.Div([\n",
" # Create an division for
adding dropdown helper text for report type \n",
" html.Div(\n",
" [\n",
" html.H2('Report Type:',
style={'margin-right': '2em'})\n",
" ]\n",
" ),\n",
" # TODO2: Add a dropdown\n",
" dcc.Dropdown(id='input-type',\
n",
" options=[\n",
"
{'label': 'Yearly Airline Performance Report', 'value': 'OPT1'},\n",
"
{'label': 'Yearly Airline Delay Report', 'value': 'OPT2'}\n",
" ],\n",
"
placeholder='Select a report type',\n",
"
style={'width':'80%', 'padding':'3px', 'font-size':'20px', 'text-align-
last':'center'}\n",
" )\n",
" # Place them next to each other
using the division style\n",
" ],
style={'display':'flex'}),\n",
" \n",
" # Add next division \n",
" html.Div([\n",
" # Create an division for adding
dropdown helper text for choosing year\n",
" html.Div(\n",
" [\n",
" html.H2('Choose Year:',
style={'margin-right': '2em'})\n",
" ]\n",
" ),\n",
" dcc.Dropdown(id='input-
year', \n",
" # Update dropdown
values using list comphrehension\n",
"
options=[{'label': i, 'value': i} for i in year_list],\n",
"
placeholder=\"Select a year\",\n",
"
style={'width':'80%', 'padding':'3px', 'font-size': '20px', 'text-align-
last' : 'center'}),\n",
" # Place them next to each
other using the division style\n",
" ], style={'display':
'flex'}) \n",
" ]),\n",
" \n",
" # Add Computed graphs\n",
" # REVIEW3: Observe how we add an empty
division and providing an id that will be updated during callback\n",
" html.Div([ ], id='plot1'),\n",
" \n",
" html.Div([\n",
" html.Div([ ], id='plot2'),\
n",
" html.Div([ ], id='plot3')\
n",
" ], \n",
" style={'display': 'flex'}),\
n",
" \n",
" # TODO3: Add a division with two empty
divisions inside. See above disvision for example.\n",
" html.Div([\n",
" html.Div([ ], id='plot4'),\
n",
" html.Div([ ], id='plot5')\
n",
" ], \n",
" style={'display': 'flex'})\
n",
" ])\n",
" )\n",
"\n",
"# Callback function definition\n",
"# TODO4: Add 5 ouput components\n",
"@app.callback( [Output(component_id='plot1',
component_property='children'),\n",
" Output(component_id='plot2',
component_property='children'),\n",
" Output(component_id='plot3',
component_property='children'),\n",
" Output(component_id='plot4',
component_property='children'),\n",
" Output(component_id='plot5',
component_property='children')\n",
" ],\n",
" [Input(component_id='input-type',
component_property='value'),\n",
" Input(component_id='input-year',
component_property='value')\n",
" ],\n",
" # REVIEW4: Holding output state till user enters all
the form information. In this case, it will be chart type and year\n",
" [State(\"plot1\", 'children'), \n",
" State(\"plot2\", \"children\"),\n",
" State(\"plot3\", \"children\"), \n",
" State(\"plot4\", \"children\"),\n",
" State(\"plot5\", \"children\")\n",
" ])\n",
"\n",
"# Add computation to callback function and return graph\n",
"def get_graph(chart, year, children1, children2, c3, c4, c5):\n",
" \n",
" # Select data\n",
" df = airline_data[airline_data['Year']==int(year)]\n",
" \n",
" if chart == 'OPT1':\n",
" # Compute required information for creating graph from the
data\n",
" bar_data, line_data, div_data, map_data, tree_data =
compute_data_choice_1(df)\n",
" \n",
" # Number of flights under different cancellation
categories\n",
" bar_fig = px.bar(bar_data, x='Month', y='Flights',
color='CancellationCode', title='Monthly Flight Cancellation')\n",
" \n",
" # TODO5: Average flight time by reporting airline\n",
" line_fig = px.line(line_data, x='Month', y='AirTime',
color='Reporting_Airline', title='Average monthly flight time (minutes) by
airline')\n",
" \n",
" # Percentage of diverted airport landings per reporting
airline\n",
" pie_fig = px.pie(div_data, values='Flights',
names='Reporting_Airline', title='% of flights by reporting airline')\n",
" \n",
" # REVIEW5: Number of flights flying from each state using
choropleth\n",
" map_fig = px.choropleth(map_data, # Input data\n",
" locations='OriginState', \n",
" color='Flights', \n",
" hover_data=['OriginState', 'Flights'], \n",
" locationmode = 'USA-states', # Set to plot as USA
States\n",
" color_continuous_scale='GnBu',\n",
" range_color=[0, map_data['Flights'].max()]) \n",
" map_fig.update_layout(\n",
" title_text = 'Number of flights from origin
state', \n",
" geo_scope='usa') # Plot only the USA instead of
globe\n",
" \n",
" # TODO6: Number of flights flying to each state from each
reporting airline\n",
" tree_fig = px.treemap(tree_data, path=['DestState',
'Reporting_Airline'], \n",
" values='Flights',\n",
" color='Flights',\n",
" color_continuous_scale='RdBu',\n",
" title='Flight count by airline to destination
state'\n",
" )\n",
" \n",
" # REVIEW6: Return dcc.Graph component to the empty
division\n",
" return [dcc.Graph(figure=tree_fig), \n",
" dcc.Graph(figure=pie_fig),\n",
" dcc.Graph(figure=map_fig),\n",
" dcc.Graph(figure=bar_fig),\n",
" dcc.Graph(figure=line_fig)\n",
" ]\n",
" else:\n",
" # REVIEW7: This covers chart type 2 and we have completed
this exercise under Flight Delay Time Statistics Dashboard section\n",
" # Compute required information for creating graph from the
data\n",
" avg_car, avg_weather, avg_NAS, avg_sec, avg_late =
compute_data_choice_2(df)\n",
" \n",
" # Create graph\n",
" carrier_fig = px.line(avg_car, x='Month',
y='CarrierDelay', color='Reporting_Airline', title='Average carrrier delay
time (minutes) by airline')\n",
" weather_fig = px.line(avg_weather, x='Month',
y='WeatherDelay', color='Reporting_Airline', title='Average weather delay
time (minutes) by airline')\n",
" nas_fig = px.line(avg_NAS, x='Month', y='NASDelay',
color='Reporting_Airline', title='Average NAS delay time (minutes) by
airline')\n",
" sec_fig = px.line(avg_sec, x='Month', y='SecurityDelay',
color='Reporting_Airline', title='Average security delay time (minutes) by
airline')\n",
" late_fig = px.line(avg_late, x='Month',
y='LateAircraftDelay', color='Reporting_Airline', title='Average late
aircraft delay time (minutes) by airline')\n",
" \n",
" return[dcc.Graph(figure=carrier_fig), \n",
" dcc.Graph(figure=weather_fig), \n",
" dcc.Graph(figure=nas_fig), \n",
" dcc.Graph(figure=sec_fig), \n",
" dcc.Graph(figure=late_fig)]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [30/Apr/2021 11:33:27] \"\u001b[37mGET
/_shutdown_33c9066e-ecae-4069-bfbf-3cb5b5baf95f HTTP/1.1\u001b[0m\" 200 -\
n",
" * Running on http://localhost:8050/ (Press CTRL+C to quit)\n",
"127.0.0.1 - - [30/Apr/2021 11:33:28] \"\u001b[37mGET
/_alive_33c9066e-ecae-4069-bfbf-3cb5b5baf95f HTTP/1.1\u001b[0m\" 200 -\n"
]
},
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"100%\"\n",
" height=\"650\"\n",
" src=\"http://localhost:8050/\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f9b9bf87400>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [30/Apr/2021 11:33:28] \"\u001b[37mGET / HTTP/1.1\
u001b[0m\" 200 -\n",
"127.0.0.1 - - [30/Apr/2021 11:33:28] \"\u001b[37mGET /_dash-
dependencies HTTP/1.1\u001b[0m\" 200 -\n",
"127.0.0.1 - - [30/Apr/2021 11:33:28] \"\u001b[37mGET /_dash-layout
HTTP/1.1\u001b[0m\" 200 -\n",
"127.0.0.1 - - [30/Apr/2021 11:33:28] \"\u001b[35m\u001b[1mPOST
/_dash-update-component HTTP/1.1\u001b[0m\" 500 -\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\
u001b[1;31m----------------------------------------------------------------
-----------\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m
Traceback (most recent call last)\n",
"\u001b[1;32m<ipython-input-13-05ca51121c43>\u001b[0m in \
u001b[0;36mget_graph\u001b[1;34m(\n",
" chart=None,\n",
" year=None,\n",
" children1=[],\n",
" children2=[],\n",
" c3=[],\n",
" c4=[],\n",
" c5=[]\n",
")\u001b[0m\n",
"\u001b[0;32m 87\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;32m 88\u001b[0m \u001b[1;31m# Select data\
u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\
u001b[0m\n",
"\u001b[1;32m---> 89\u001b[1;33m \u001b[0mdf\u001b[0m \
u001b[1;33m=\u001b[0m \u001b[0mairline_data\u001b[0m\u001b[1;33m[\
u001b[0m\u001b[0mairline_data\u001b[0m\u001b[1;33m[\u001b[0m\
u001b[1;34m'Year'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m==\u001b[0m\
u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0myear\u001b[0m\
u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\
u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0m \u001b[0;36mdf\u001b[0m \u001b[1;34m= \
u001b[1;36mundefined\u001b[0m\u001b[0m\u001b[1;34m\n",
" \u001b[0m\u001b[1;36mglobal\u001b[0m \
u001b[0;36mairline_data\u001b[0m \u001b[1;34m= Unnamed: 0 Year
Quarter Month DayofMonth DayOfWeek FlightDate \\\n",
"0 1295781 1998 2 4 2 4
1998-04-02 \n",
"1 1125375 2013 2 5 13 1
2013-05-13 \n",
"2 118824 1993 3 9 25 6
1993-09-25 \n",
"3 634825 1994 4 11 12 6
1994-11-12 \n",
"4 1888125 2017 3 8 17 4
2017-08-17 \n",
"... ... ... ... ... ... ...
... \n",
"26995 821542 2017 1 1 24 2
2017-01-24 \n",
"26996 1910565 2013 2 6 27 4
2013-06-27 \n",
"26997 9055 2016 3 8 26 5
2016-08-26 \n",
"26998 84136 2009 3 8 8 6
2009-08-08 \n",
"26999 113029 1993 3 7 17 6
1993-07-17 \n",
"\n",
" Reporting_Airline DOT_ID_Reporting_Airline
IATA_CODE_Reporting_Airline \\\n",
"0 AS 19930
AS \n",
"1 EV 20366
EV \n",
"2 UA 19977
UA \n",
"3 HP 19991
HP \n",
"4 UA 19977
UA \n",
"... ... ...
... \n",
"26995 DL 19790
DL \n",
"26996 B6 20409
B6 \n",
"26997 AA 19805
AA \n",
"26998 YV 20378
YV \n",
"26999 DL 19790
DL \n",
"\n",
" ... Div4WheelsOff Div4TailNum Div5Airport
Div5AirportID \\\n",
"0 ... NaN NaN NaN
NaN \n",
"1 ... NaN NaN NaN
NaN \n",
"2 ... NaN NaN NaN
NaN \n",
"3 ... NaN NaN NaN
NaN \n",
"4 ... NaN NaN NaN
NaN \n",