Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/10. Applied Data Science Capstone/04. Interactive Visual Analytics/04. Interactive Visual Analytics - Plotly Dash dashboard_ spacex_dash_app.py
Views: 4598
# Import required libraries1import pandas as pd23import dash4from dash import html5from dash import dcc6from dash.dependencies import Input, Output78import plotly.express as px910# Read the airline data into pandas dataframe11spacex_df = pd.read_csv("4. Data Visualization - Plotly Dash data: spacex_launch_dash.csv")1213#Minimum and maximum Payload masses for the range slider14max_payload = spacex_df['Payload Mass (kg)'].max()15min_payload = spacex_df['Payload Mass (kg)'].min()1617# Create a dash application18app = dash.Dash(__name__)192021#Get the launch sites from the spacex_df dataframe to use in the dropdown list22launch_sites = []23launch_sites.append({'label': 'All Sites', 'value': 'All Sites'})24for launch_site in spacex_df['Launch Site'].unique().tolist():25launch_sites.append({'label': launch_site, 'value': launch_site})262728293031# Create an app layout32app.layout = html.Div(children=[html.H1('SpaceX Launch Records Dashboard',33style={'textAlign': 'center', 'color': '#503D36',34'font-size': 40}),3536# TASK 1: Add a dropdown list to enable Launch Site selection37dcc.Dropdown(id='site-dropdown',38options = launch_sites,39value='All Sites',40placeholder="Launch Site",41searchable=True42),4344html.Br(),4546# TASK 2: Add a pie chart to show the total successful launches count for all sites47# If a specific launch site was selected, show the Success vs. Failed counts for the site48html.Div(dcc.Graph(id='success-pie-chart')), #this id will be used in the callback function to change the pie chart4950html.Br(),5152html.P("Payload range (Kg):"),5354# TASK 3: Add a slider to select payload range55dcc.RangeSlider(id='payload-slider',56min=0, max=10000, step=1000,57marks={0: '0', 2500: '2500', 5000: '5000', 7500: '7500', 10000: '10000'},58value=[min_payload, max_payload]),5960616263# TASK 4: Add a scatter chart to show the correlation between payload and launch success64html.Div(dcc.Graph(id='success-payload-scatter-chart')),65])6667# TASK 2:68# Add a callback function for `site-dropdown` as input, `success-pie-chart` as output69@app.callback(Output(component_id='success-pie-chart', component_property='figure'),70Input(component_id='site-dropdown', component_property='value'))7172def get_pie_chart(entered_site):73filtered_df = spacex_df[spacex_df['Launch Site'] == entered_site]74if entered_site == 'All Sites':75fig = px.pie(spacex_df, values='class', names='Launch Site', title='Total Success Launches by Site')76return fig77else:78# return the outcomes pie chart for a selected site79site_df = filtered_df.groupby(['Launch Site', 'class']).size().reset_index(name='class count')80fig = px.pie(site_df,values='class count',names='class',title=f'Total Success Launches for site {entered_site}')81return fig82838485# TASK 4:86# Add a callback function for `site-dropdown` and `payload-slider` as inputs, `success-payload-scatter-chart` as output87@app.callback(Output(component_id='success-payload-scatter-chart', component_property='figure'),88[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 list899091def get_scatter_chart(entered_site, payload_slider):92low, high = payload_slider93slide=(spacex_df['Payload Mass (kg)'] > low) & (spacex_df['Payload Mass (kg)'] < high)94dropdown_scatter=spacex_df[slide]9596#If All sites are selected, render a scatter plot to display all values for variables Payload Mass (kg) and class.97#Point colour is set to the booster version category98if entered_site == 'All Sites':99fig = px.scatter(100dropdown_scatter, x='Payload Mass (kg)', y='class',101hover_data=['Booster Version'],102color='Booster Version Category',103title='Correlation between Payload and Success for all Sites')104return fig105else:106#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.107dropdown_scatter = dropdown_scatter[spacex_df['Launch Site'] == entered_site]108fig=px.scatter(109dropdown_scatter,x='Payload Mass (kg)', y='class',110hover_data=['Booster Version'],111color='Booster Version Category',112title = f'Success by Payload Size for site {entered_site}')113return fig114115# Run the app116if __name__ == '__main__':117app.run_server()118119120