import dash
from dash import dcc, html
import pandas as pd
import plotly.express as px
# Load the Gapminder dataset
df = px.data.gapminder()
# Calculate global average life expectancy and GDP per capita for the cards
avg_life_exp = df['lifeExp'].mean()
avg_gdp_per_capita = df['gdpPercap'].mean()
# Initialize the Dash app
app = dash.Dash(__name__)
app.layout = html.Div([
# Dashboard Title
html.H1("Global Development Dashboard", style={'textAlign': 'center'}),
# Top cards for summary metrics
html.Div([
html.Div([
html.H3("Global Average Life Expectancy"),
html.P(f"{avg_life_exp:.2f} years", style={'fontSize': '24px', 'color': '#4CAF50'})
], className="card", style={'padding': '20px', 'border': '1px solid #ddd', 'borderRadius': '10px', 'width': '30%'}),
html.Div([
html.H3("Average GDP per Capita"),
html.P(f"${avg_gdp_per_capita:,.2f}", style={'fontSize': '24px', 'color': '#4CAF50'})
], className="card", style={'padding': '20px', 'border': '1px solid #ddd', 'borderRadius': '10px', 'width': '30%'})
], style={'display': 'flex', 'justifyContent': 'space-around', 'margin': '20px 0'}),
# Bubble chart for Life Expectancy vs GDP per capita
html.Div([
dcc.Graph(id="bubble-chart")
])
])
# Bubble chart callback
@app.callback(
dash.dependencies.Output("bubble-chart", "figure"),
[dash.dependencies.Input("bubble-chart", "figure")]
)
def update_chart(_):
# Filter data for the latest year in the dataset
df_latest = df[df['year'] == df['year'].max()]
# Create bubble chart
fig = px.scatter(df_latest,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
title="Life Expectancy vs GDP per Capita",
labels={"gdpPercap": "GDP per Capita", "lifeExp": "Life Expectancy"},
template="plotly_white")
fig.update_layout(margin=dict(t=50, l=50, r=50, b=50))
return fig
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)