Spaces:
Sleeping
Sleeping
Upload spacex_dash_app.py
Browse files- spacex_dash_app.py +86 -0
spacex_dash_app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import pandas as pd
|
3 |
+
import dash
|
4 |
+
import dash_html_components as html
|
5 |
+
import dash_core_components as dcc
|
6 |
+
from dash.dependencies import Input, Output, State
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
import plotly.express as px
|
9 |
+
from dash import no_update
|
10 |
+
|
11 |
+
# Read the airline data into pandas dataframe
|
12 |
+
spacex_df = pd.read_csv("spacex_launch_dash.csv")
|
13 |
+
max_payload = spacex_df['Payload Mass (kg)'].max()
|
14 |
+
min_payload = spacex_df['Payload Mass (kg)'].min()
|
15 |
+
|
16 |
+
# Create a dash application
|
17 |
+
app = dash.Dash(__name__)
|
18 |
+
|
19 |
+
# Create an app layout
|
20 |
+
launch_sites = []
|
21 |
+
launch_sites.append({'label': 'All Sites', 'value': 'All Sites'})
|
22 |
+
for item in spacex_df["Launch Site"].value_counts().index:
|
23 |
+
launch_sites.append({'label': item, 'value': item})
|
24 |
+
app.layout = html.Div(children=[html.H1('SpaceX Launch Records Dashboard',
|
25 |
+
style={'textAlign': 'center', 'color': '#503D36',
|
26 |
+
'font-size': 40}),
|
27 |
+
# TASK 1: Add a dropdown list to enable Launch Site selection
|
28 |
+
# The default select value is for ALL sites
|
29 |
+
dcc.Dropdown(id='site-dropdown', options = launch_sites, value = 'All Sites', placeholder = "Select a Launch Site here", searchable = True),
|
30 |
+
html.Br(),
|
31 |
+
|
32 |
+
# TASK 2: Add a pie chart to show the total successful launches count for all sites
|
33 |
+
# If a specific launch site was selected, show the Success vs. Failed counts for the site
|
34 |
+
html.Div(dcc.Graph(id='success-pie-chart')),
|
35 |
+
html.Br(),
|
36 |
+
|
37 |
+
html.P("Payload range (Kg):"),
|
38 |
+
# TASK 3: Add a slider to select payload range
|
39 |
+
dcc.RangeSlider(id='payload-slider', min = 0, max = 10000, step = 1000, value = [min_payload, max_payload], marks={ 2500: {'label': '2500 (Kg)'}, 5000: {'label': '5000 (Kg)'}, 7500: {'label': '7500 (Kg)'}}),
|
40 |
+
|
41 |
+
# TASK 4: Add a scatter chart to show the correlation between payload and launch success
|
42 |
+
html.Div(dcc.Graph(id='success-payload-scatter-chart')),
|
43 |
+
])
|
44 |
+
|
45 |
+
# TASK 2:
|
46 |
+
# Add a callback function for `site-dropdown` as input, `success-pie-chart` as output
|
47 |
+
@app.callback( Output(component_id='success-pie-chart', component_property='figure'),
|
48 |
+
Input(component_id='site-dropdown', component_property='value')
|
49 |
+
)
|
50 |
+
# Add computation to callback function and return graph
|
51 |
+
def select(inputt):
|
52 |
+
if inputt == 'All Sites':
|
53 |
+
new_df = spacex_df.groupby(['Launch Site'])["class"].sum().to_frame()
|
54 |
+
new_df = new_df.reset_index()
|
55 |
+
fig = px.pie(new_df, values='class', names='Launch Site', title='Total Success Launches by Site')
|
56 |
+
else:
|
57 |
+
new_df = spacex_df[spacex_df["Launch Site"] == inputt]["class"].value_counts().to_frame()
|
58 |
+
new_df["name"] = ["Failure", "Success"]
|
59 |
+
fig = px.pie(new_df, values='class', names='name', title='Total Success Launches for ' + inputt)
|
60 |
+
return fig
|
61 |
+
|
62 |
+
# TASK 4:
|
63 |
+
# Add a callback function for `site-dropdown` and `payload-slider` as inputs, `success-payload-scatter-chart` as output
|
64 |
+
@app.callback( Output(component_id='success-payload-scatter-chart', component_property='figure'),
|
65 |
+
Input(component_id='site-dropdown', component_property='value'), Input(component_id='payload-slider', component_property='value')
|
66 |
+
)
|
67 |
+
def scatter(input1, input2):
|
68 |
+
print(input1)
|
69 |
+
print(input2)
|
70 |
+
if input1 == 'All Sites':
|
71 |
+
new_df = spacex_df
|
72 |
+
new_df2 = new_df[new_df["Payload Mass (kg)"] >= input2[0]]
|
73 |
+
new_df3 = new_df2[new_df["Payload Mass (kg)"] <= input2[1]]
|
74 |
+
fig2 = px.scatter(new_df3, y="class", x="Payload Mass (kg)", color="Booster Version Category")
|
75 |
+
else:
|
76 |
+
new_df = spacex_df[spacex_df["Launch Site"] == input1]
|
77 |
+
new_df2 = new_df[new_df["Payload Mass (kg)"] >= input2[0]]
|
78 |
+
new_df3 = new_df2[new_df["Payload Mass (kg)"] <= input2[1]]
|
79 |
+
#new_df2 = new_df[new_df["Payload Mass (kg)"] >= input2[0] & new_df["Payload Mass (kg)"] <= input2[1]]
|
80 |
+
fig2 = px.scatter(new_df3, y="class", x="Payload Mass (kg)", color="Booster Version Category")
|
81 |
+
return fig2
|
82 |
+
|
83 |
+
|
84 |
+
# Run the app
|
85 |
+
if __name__ == '__main__':
|
86 |
+
app.run_server()
|