3
3
import plotly .graph_objects as go
4
4
from data import happiness_data
5
5
from utils import FACTORS , COLORS
6
-
6
+ from random import sample
7
7
8
8
@callback (
9
9
Output ("world-map" , "figure" ),
@@ -30,43 +30,96 @@ def update_table(country1, country2, year):
30
30
output_df = happiness_data .loc [happiness_data ["Year" ] == year ]
31
31
top_3 = output_df .head (3 )['Country' ].tolist ()
32
32
bottom_3 = output_df .tail (3 )['Country' ].tolist ()
33
+ highlight_color = COLORS [1 ]
34
+ maximum_num_rows = 13
35
+
33
36
if country1 and country2 :
34
37
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
+
35
52
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 ]
36
55
style = [
37
56
{
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'
40
60
}
41
61
]
42
62
elif country1 :
43
63
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
+
44
79
country_rank = output_df .loc [output_df ["Country" ] == country1 , "Overall rank" ].tolist ()[0 ]
45
80
output_df = output_df .query ("Country in @countries_list" )
46
81
output_df = output_df [["Overall rank" , "Country" , "Score" ]]
47
82
style = [
48
83
{
49
84
"if" : {"filter_query" : "{{Overall rank}} = {}" .format (country_rank )},
50
- "font-weight" : "bold"
85
+ 'backgroundColor' : highlight_color ,
86
+ 'color' : 'white'
51
87
}
52
88
]
53
89
elif country2 :
54
90
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
+
55
106
country_rank = output_df .loc [output_df ["Country" ] == country2 , "Overall rank" ].tolist ()[0 ]
56
107
output_df = output_df .query ("Country in @countries_list" )
57
108
output_df = output_df [["Overall rank" , "Country" , "Score" ]]
58
109
style = [
59
110
{
60
111
"if" : {"filter_query" : "{{Overall rank}} = {}" .format (country_rank )},
61
- "font-weight" : "bold"
112
+ 'backgroundColor' : highlight_color ,
113
+ 'color' : 'white'
62
114
}
63
115
]
64
116
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 )
66
118
style = [
67
119
{
68
120
"if" : {"filter_query" : "{{Overall rank}} = {}" .format (output_df ["Overall rank" ].min ())},
69
- "font-weight" : "bold"
121
+ 'backgroundColor' : highlight_color ,
122
+ 'color' : 'white'
70
123
}
71
124
]
72
125
0 commit comments