Skip to content

Commit 21a80f8

Browse files
alexzhang0825jy1909
andauthoredApr 19, 2024
Reformatting the table for better visuals (#89)
* Modified .gitignore so that now it ignores pycache and rhistory * The table now highlights the rows corresopnding to the selected countries yellow. If no country is selected, the first row (highest rank) is highlighted * The table now shows enough rows to have the same height as the map. By default, it shows 13 rows, and if there is no enough rows after filtering, random rows will be selected to make up * The highlighted countries on the table now no longer are bolded. The header of the table showing the columns are now bolded, and have a blue background color matching the overall color scheme * Fixed code with country names with spaces, fixed filler countries * Removed unnecessary files --------- Close #90 Co-authored-by: Jerry Yu <jyu565@gmail.com>
1 parent 227739e commit 21a80f8

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed
 

‎.gitignore

-1
This file was deleted.

‎src/callbacks/charts.py

+60-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import plotly.graph_objects as go
44
from data import happiness_data
55
from utils import FACTORS, COLORS
6-
6+
from random import sample
77

88
@callback(
99
Output("world-map", "figure"),
@@ -30,43 +30,96 @@ def update_table(country1, country2, year):
3030
output_df = happiness_data.loc[happiness_data["Year"] == year]
3131
top_3 = output_df.head(3)['Country'].tolist()
3232
bottom_3 = output_df.tail(3)['Country'].tolist()
33+
highlight_color = COLORS[1]
34+
maximum_num_rows = 13
35+
3336
if country1 and country2:
3437
countries_list = top_3 + bottom_3 + [country1, country2]
38+
placeholders_num = maximum_num_rows - len(set(countries_list))
39+
40+
full_countries = output_df['Country'].to_list()
41+
filtered = [item for item in full_countries if item not in countries_list]
42+
43+
if placeholders_num % 2:
44+
add_top = filtered[:placeholders_num//2+1]
45+
add_bottom = filtered[-placeholders_num//2:]
46+
else:
47+
add_top = filtered[:placeholders_num//2]
48+
add_bottom = filtered[-placeholders_num//2:]
49+
50+
countries_list = countries_list + add_top + add_bottom
51+
3552
output_df = output_df[["Overall rank", "Country", "Score"]].query("Country in @countries_list")
53+
rank_country_1 = output_df.loc[output_df["Country"] == country1, "Overall rank"].tolist()[0]
54+
rank_country_2 = output_df.loc[output_df["Country"] == country2, "Overall rank"].tolist()[0]
3655
style = [
3756
{
38-
"if": {"filter_query": "{{Overall rank}} = {}".format(output_df["Overall rank"].min())},
39-
"font-weight": "bold",
57+
"if": {"filter_query": f"{{Overall rank}} = {rank_country_1} || {{Overall rank}} = {rank_country_2}"},
58+
'backgroundColor': highlight_color,
59+
'color': 'white'
4060
}
4161
]
4262
elif country1:
4363
countries_list = top_3 + bottom_3 + [country1]
64+
65+
placeholders_num = maximum_num_rows - len(set(countries_list))
66+
67+
full_countries = output_df['Country'].to_list()
68+
filtered = [item for item in full_countries if item not in countries_list]
69+
70+
if placeholders_num % 2:
71+
add_top = filtered[:placeholders_num//2+1]
72+
add_bottom = filtered[-placeholders_num//2:]
73+
else:
74+
add_top = filtered[:placeholders_num//2]
75+
add_bottom = filtered[-placeholders_num//2:]
76+
77+
countries_list = countries_list + add_top + add_bottom
78+
4479
country_rank = output_df.loc[output_df["Country"] == country1, "Overall rank"].tolist()[0]
4580
output_df = output_df.query("Country in @countries_list")
4681
output_df = output_df[["Overall rank", "Country", "Score"]]
4782
style = [
4883
{
4984
"if": {"filter_query": "{{Overall rank}} = {}".format(country_rank)},
50-
"font-weight": "bold"
85+
'backgroundColor': highlight_color,
86+
'color': 'white'
5187
}
5288
]
5389
elif country2:
5490
countries_list = top_3 + bottom_3 + [country2]
91+
92+
placeholders_num = maximum_num_rows - len(set(countries_list))
93+
94+
full_countries = output_df['Country'].to_list()
95+
filtered = [item for item in full_countries if item not in countries_list]
96+
97+
if placeholders_num % 2:
98+
add_top = filtered[:placeholders_num//2+1]
99+
add_bottom = filtered[-placeholders_num//2:]
100+
else:
101+
add_top = filtered[:placeholders_num//2]
102+
add_bottom = filtered[-placeholders_num//2:]
103+
104+
countries_list = countries_list + add_top + add_bottom
105+
55106
country_rank = output_df.loc[output_df["Country"] == country2, "Overall rank"].tolist()[0]
56107
output_df = output_df.query("Country in @countries_list")
57108
output_df = output_df[["Overall rank", "Country", "Score"]]
58109
style = [
59110
{
60111
"if": {"filter_query": "{{Overall rank}} = {}".format(country_rank)},
61-
"font-weight": "bold"
112+
'backgroundColor': highlight_color,
113+
'color': 'white'
62114
}
63115
]
64116
else:
65-
output_df = output_df[["Overall rank", "Country", "Score"]].head(10)
117+
output_df = output_df[["Overall rank", "Country", "Score"]].head(maximum_num_rows)
66118
style = [
67119
{
68120
"if": {"filter_query": "{{Overall rank}} = {}".format(output_df["Overall rank"].min())},
69-
"font-weight": "bold"
121+
'backgroundColor': highlight_color,
122+
'color': 'white'
70123
}
71124
]
72125

‎src/components/charts.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from dash import dash_table, html, dcc
22
import dash_bootstrap_components as dbc
3+
from utils import COLORS
34

45

56
world_map = dbc.Card(children=[html.H5("World Map of Happiness Scores"),
67
dcc.Graph(id="world-map")],
78
body=True)
89
rank_table = dbc.Card(children=[html.H5("Country Rankings"),
910
html.Br(),
10-
dash_table.DataTable(id="rank-table")],
11+
dash_table.DataTable(id="rank-table",
12+
style_header={'backgroundColor': COLORS[0],
13+
'color': 'white',
14+
'textAlign': 'center',
15+
"font-weight": "bold"},
16+
style_data={'textAlign': 'center'})],
1117
body=True,
1218
style={"height":"100%"})
1319
line_chart = dbc.Card(children=[html.H5("World Map of Happiness Scores"),

‎src/utils/global_variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
FACTORS = ["GDP per capita", "Social support", "Healthy life expectancy",
22
"Freedom to make life choices", "Generosity", "Perceptions of corruption"]
3-
COLORS = ['#1289ce', '#f2c201', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52']
3+
COLORS = ['#1289ce', '#f2c201', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52']

0 commit comments

Comments
 (0)