CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
DanielBarnes18

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: DanielBarnes18/IBM-Data-Science-Professional-Certificate
Path: blob/main/10. Applied Data Science Capstone/04. Interactive Visual Analytics/spacex_dash_app.py
Views: 4598
1
# Import required libraries
2
import pandas as pd
3
4
import dash
5
from dash import html
6
from dash import dcc
7
from dash.dependencies import Input, Output
8
9
import plotly.express as px
10
11
# Read the airline data into pandas dataframe
12
spacex_df = pd.read_csv("spacex_launch_dash.csv")
13
14
#Minimum and maximum Payload masses for the range slider
15
max_payload = spacex_df['Payload Mass (kg)'].max()
16
min_payload = spacex_df['Payload Mass (kg)'].min()
17
18
# Create a dash application
19
app = dash.Dash(__name__)
20
21
22
#Get the launch sites from the spacex_df dataframe to use in the dropdown list
23
launch_sites = []
24
launch_sites.append({'label': 'All Sites', 'value': 'All Sites'})
25
for launch_site in spacex_df['Launch Site'].unique().tolist():
26
launch_sites.append({'label': launch_site, 'value': launch_site})
27
28
29
30
31
32
# Create an app layout
33
app.layout = html.Div(children=[html.H1('SpaceX Launch Records Dashboard',
34
style={'textAlign': 'center', 'color': '#503D36',
35
'font-size': 40}),
36
37
# TASK 1: Add a dropdown list to enable Launch Site selection
38
dcc.Dropdown(id='site-dropdown',
39
options = launch_sites,
40
value='All Sites',
41
placeholder="Launch Site",
42
searchable=True
43
),
44
45
html.Br(),
46
47
# TASK 2: Add a pie chart to show the total successful launches count for all sites
48
# If a specific launch site was selected, show the Success vs. Failed counts for the site
49
html.Div(dcc.Graph(id='success-pie-chart')), #this id will be used in the callback function to change the pie chart
50
51
html.Br(),
52
53
html.P("Payload range (Kg):"),
54
55
# TASK 3: Add a slider to select payload range
56
dcc.RangeSlider(id='payload-slider',
57
min=0, max=10000, step=1000,
58
marks={0: '0', 2500: '2500', 5000: '5000', 7500: '7500', 10000: '10000'},
59
value=[min_payload, max_payload]),
60
61
62
63
64
# TASK 4: Add a scatter chart to show the correlation between payload and launch success
65
html.Div(dcc.Graph(id='success-payload-scatter-chart')),
66
])
67
68
# TASK 2:
69
# Add a callback function for `site-dropdown` as input, `success-pie-chart` as output
70
@app.callback(Output(component_id='success-pie-chart', component_property='figure'),
71
Input(component_id='site-dropdown', component_property='value'))
72
73
def get_pie_chart(entered_site):
74
filtered_df = spacex_df[spacex_df['Launch Site'] == entered_site]
75
if entered_site == 'All Sites':
76
fig = px.pie(spacex_df, values='class', names='Launch Site', title='Total Success Launches by Site')
77
return fig
78
else:
79
# return the outcomes pie chart for a selected site
80
site_df = filtered_df.groupby(['Launch Site', 'class']).size().reset_index(name='class count')
81
fig = px.pie(site_df,values='class count',names='class',title=f'Total Success Launches for site {entered_site}')
82
return fig
83
84
85
86
# TASK 4:
87
# Add a callback function for `site-dropdown` and `payload-slider` as inputs, `success-payload-scatter-chart` as output
88
@app.callback(Output(component_id='success-payload-scatter-chart', component_property='figure'),
89
[Input(component_id='site-dropdown', component_property='value'), Input(component_id='payload-slider', component_property='value')]) #note the 2 inputs, so they are in a list
90
91
92
def get_scatter_chart(entered_site, payload_slider):
93
low, high = payload_slider
94
slide=(spacex_df['Payload Mass (kg)'] > low) & (spacex_df['Payload Mass (kg)'] < high)
95
dropdown_scatter=spacex_df[slide]
96
97
#If All sites are selected, render a scatter plot to display all values for variables Payload Mass (kg) and class.
98
#Point colour is set to the booster version category
99
if entered_site == 'All Sites':
100
fig = px.scatter(
101
dropdown_scatter, x='Payload Mass (kg)', y='class',
102
hover_data=['Booster Version'],
103
color='Booster Version Category',
104
title='Correlation between Payload and Success for all Sites')
105
return fig
106
else:
107
#If a specific site is selected, filter the spacex_df dataframe first, then render a scatter plot to display the same as for all sites.
108
dropdown_scatter = dropdown_scatter[spacex_df['Launch Site'] == entered_site]
109
fig=px.scatter(
110
dropdown_scatter,x='Payload Mass (kg)', y='class',
111
hover_data=['Booster Version'],
112
color='Booster Version Category',
113
title = f'Success by Payload Size for site {entered_site}')
114
return fig
115
116
# Run the app
117
if __name__ == '__main__':
118
app.run_server()
119
120