-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
151 lines (107 loc) · 6.18 KB
/
dashboard.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# %%
from plotly.data import gapminder
from dash import dcc, html, Dash, callback, Input, Output
import plotly.express as px
import plotly.graph_objects as go
# %%
css = ["https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css", ]
app = Dash(name="Gapminder Dashboard", external_stylesheets=css)
# %%
################### DATASET ####################################
gapminder_df = gapminder(datetimes=True, centroids=True, pretty_names=True)
gapminder_df["Year"] = gapminder_df.Year.dt.year
gapminder_df.head()
# %%
#################### PLOTLY/DASH BUG FIX ########################
go.Figure(layout=dict(template='plotly'))
#################### CHARTS #####################################
def create_table():
fig = go.Figure(data=[go.Table(
header=dict(values=gapminder_df.columns, align='left'),
cells=dict(values=gapminder_df.values.T, align='left'))])
fig.update_layout(paper_bgcolor="#e5ecf6", margin={"t":0, "l":0, "r":0, "b":0}, height=700)
return fig
def create_population_chart(continent="Asia", year=1952):
filtered_df = gapminder_df[(gapminder_df.Continent==continent) & (gapminder_df.Year==year)]
filtered_df = filtered_df.sort_values(by="Population", ascending=False).head(15)
fig = px.bar(filtered_df, x="Country", y="Population", color="Country",
title="Country {} for {} Continent in {}".format("Population", continent, year),
text_auto=True)
fig.update_layout(paper_bgcolor="#e5ecf6", height=600)
return fig
def create_gdp_chart(continent="Asia", year=1952):
filtered_df = gapminder_df[(gapminder_df.Continent==continent) & (gapminder_df.Year==year)]
filtered_df = filtered_df.sort_values(by="GDP per Capita", ascending=False).head(15)
fig = px.bar(filtered_df, x="Country", y="GDP per Capita", color="Country",
title="Country {} for {} Continent in {}".format("GDP per Capita", continent, year),
text_auto=True)
fig.update_layout(paper_bgcolor="#e5ecf6", height=600)
return fig
def create_life_exp_chart(continent="Asia", year=1952):
filtered_df = gapminder_df[(gapminder_df.Continent==continent) & (gapminder_df.Year==year)]
filtered_df = filtered_df.sort_values(by="Life Expectancy", ascending=False).head(15)
fig = px.bar(filtered_df, x="Country", y="Life Expectancy", color="Country",
title="Country {} for {} Continent in {}".format("Life Expectancy", continent, year),
text_auto=True)
fig.update_layout(paper_bgcolor="#e5ecf6", height=600)
return fig
def create_choropleth_map(variable, year):
# print('variable = ', variable, 'year = ', year)
filtered_df = gapminder_df[gapminder_df.Year==year]
fig = px.choropleth(filtered_df, color=variable,
locations="ISO Alpha Country Code", locationmode="ISO-3",
color_continuous_scale="RdYlBu", hover_data=["Country", variable],
title="{} Choropleth Map [{}]".format(variable, year))
fig.update_layout(dragmode=False, paper_bgcolor="#e5ecf6", height=600, margin={"l":0, "r":0})
return fig
# %%
##################### WIDGETS ####################################
continents = gapminder_df.Continent.unique()
years = gapminder_df.Year.unique()
cont_population = dcc.Dropdown(id="cont_population", options=continents, value="Asia", clearable=False)
year_population = dcc.Dropdown(id="year_population", options=years, value=1952, clearable=False)
cont_gdp = dcc.Dropdown(id="cont_gdp", options=continents, value="Asia", clearable=False)
year_gdp = dcc.Dropdown(id="year_gdp", options=years, value=1952, clearable=False)
cont_life_exp = dcc.Dropdown(id="cont_life_exp", options=continents, value="Asia", clearable=False)
year_life_exp = dcc.Dropdown(id="year_life_exp", options=years, value=1952, clearable=False)
year_map = dcc.Dropdown(id="year_map", options=years, value=1952, clearable=False)
var_map = dcc.Dropdown(id="var_map", options=["Population", "GDP per Capita", "Life Expectancy"],
value="Life Expectancy", clearable=False)
# %%
##################### APP LAYOUT ####################################
app.layout = html.Div([
html.Div([
html.H1("Gapminder Dataset Analysis", className="text-center fw-bold m-2"),
html.Br(),
dcc.Tabs([ # Tabs along a header row
dcc.Tab([html.Br(),
dcc.Graph(id="dataset", figure=create_table())], label="Dataset"),
dcc.Tab([html.Br(), "Continent", cont_population, "Year", year_population, html.Br(),
dcc.Graph(id="population")], label="Population"),
dcc.Tab([html.Br(), "Continent", cont_gdp, "Year", year_gdp, html.Br(),
dcc.Graph(id="gdp")], label="GDP Per Capita"),
dcc.Tab([html.Br(), "Continent", cont_life_exp, "Year", year_life_exp, html.Br(),
dcc.Graph(id="life_expectancy")], label="Life Expectancy"),
dcc.Tab([html.Br(), "Variable", var_map, "Year", year_map, html.Br(),
dcc.Graph(id="choropleth_map")], label="Choropleth Map"),
])
], className="col-8 mx-auto"),
], style={"background-color": "#e5ecf6", "height": "100vh"})
# %%
##################### CALLBACKS ####################################
@callback(Output("population", "figure"), [Input("cont_population", "value"), Input("year_population", "value")])
def update_population_chart(continent, year):
return create_population_chart(continent, year)
@callback(Output("gdp", "figure"), [Input("cont_gdp", "value"), Input("year_gdp", "value")])
def update_gdp_chart(continent, year):
return create_gdp_chart(continent, year)
@callback(Output("life_expectancy", "figure"), [Input("cont_life_exp", "value"), Input("year_life_exp", "value")])
def update_life_exp_chart(continent, year):
return create_life_exp_chart(continent, year)
@callback(Output("choropleth_map", "figure"), [Input("var_map", "value"), Input("year_map", "value"),])
def update_map(var_map, year):
return create_choropleth_map(var_map, year)
# %%
##################### START THE APP ####################################
if __name__ == "__main__":
app.run(debug=True)