-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspacex_dash_app.py
More file actions
89 lines (75 loc) · 3.12 KB
/
spacex_dash_app.py
File metadata and controls
89 lines (75 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Import required libraries
import pandas as pd
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.express as px
# Read the SpaceX launch data into pandas dataframe
spacex_df = pd.read_csv("spacex_launch_dash.csv")
max_payload = spacex_df['Payload Mass (kg)'].max()
min_payload = spacex_df['Payload Mass (kg)'].min()
# Create a dash application
app = dash.Dash(__name__)
# TASK 1: Add a Launch Site Drop-down Input Component
launch_sites = [{'label': 'All Sites', 'value': 'ALL'}] + \
[{'label': site, 'value': site} for site in spacex_df['Launch Site'].unique()]
app.layout = html.Div(children=[
html.H1('SpaceX Launch Records Dashboard',
style={'textAlign': 'center', 'color': '#503D36',
'font-size': 40}),
dcc.Dropdown(
id='site-dropdown',
options=launch_sites,
value='ALL',
placeholder="Select a Launch Site here",
searchable=True
),
html.Br(),
# TASK 2: Add a pie chart
html.Div(dcc.Graph(id='success-pie-chart')),
html.Br(),
html.P("Payload range (Kg):"),
# TASK 3: Add a RangeSlider to Select Payload
dcc.RangeSlider(
id='payload-slider',
min=0, max=10000, step=1000,
marks={0: '0', 2500: '2500', 5000: '5000', 7500: '7500', 10000: '10000'},
value=[min_payload, max_payload]
),
# TASK 4: Add a scatter chart
html.Div(dcc.Graph(id='success-payload-scatter-chart')),
])
# TASK 2 Callback: Update pie chart based on selected site
@app.callback(
Output(component_id='success-pie-chart', component_property='figure'),
Input(component_id='site-dropdown', component_property='value')
)
def update_pie_chart(selected_site):
if selected_site == 'ALL':
fig = px.pie(spacex_df, names='Launch Site', values='class',
title='Total Success Launches By Site')
else:
filtered_df = spacex_df[spacex_df['Launch Site'] == selected_site]
fig = px.pie(filtered_df, names='class',
title=f'Total Success vs Failure for site {selected_site}')
return fig
# TASK 4 Callback: Update scatter chart based on site and payload range
@app.callback(
Output(component_id='success-payload-scatter-chart', component_property='figure'),
[Input(component_id='site-dropdown', component_property='value'),
Input(component_id='payload-slider', component_property='value')]
)
def update_scatter(selected_site, payload_range):
low, high = payload_range
mask = (spacex_df['Payload Mass (kg)'] >= low) & (spacex_df['Payload Mass (kg)'] <= high)
filtered_df = spacex_df[mask]
if selected_site != 'ALL':
filtered_df = filtered_df[filtered_df['Launch Site'] == selected_site]
fig = px.scatter(filtered_df, x='Payload Mass (kg)', y='class',
color='Booster Version Category',
title='Correlation between Payload and Success',
hover_data=['Launch Site'])
return fig
# Run the app
if __name__ == '__main__':
app.run_server()