Py.Cafe

maxi.schulz/

gapminder-global-development

Global Development Insights Using Gapminder

DocsPricing
  • app.py
  • requirements.txt
app.py
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
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)