Ryan McConville
commited on
Commit
Β·
1bacdcc
1
Parent(s):
23f0448
fix sorting with medal icons
Browse files- src/populate.py +17 -2
src/populate.py
CHANGED
@@ -33,8 +33,23 @@ def get_leaderboard_df(results_path):
|
|
33 |
medal_map = {1: "π₯", 2: "π₯", 3: "π₯"}
|
34 |
|
35 |
def medal_html(rank):
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
df["Rank"] = df.index + 1
|
40 |
df["Rank"] = df["Rank"].apply(medal_html)
|
|
|
33 |
medal_map = {1: "π₯", 2: "π₯", 3: "π₯"}
|
34 |
|
35 |
def medal_html(rank):
|
36 |
+
"""Return an HTML span with the medal icon for the top 3 ranks.
|
37 |
+
|
38 |
+
The numeric rank is stored in the data-order attribute equal to the numerical rank so that
|
39 |
+
DataTables (used under-the-hood by the gradio_leaderboard component)
|
40 |
+
can sort the column by this hidden numeric value while still
|
41 |
+
displaying the pretty medal icon. For ranks > 3 we just return the
|
42 |
+
integer so the column remains fully numeric.
|
43 |
+
"""
|
44 |
+
medal = medal_map.get(rank)
|
45 |
+
if medal:
|
46 |
+
# Prepend a hidden numeric span so string sorting still works numerically.
|
47 |
+
return (
|
48 |
+
f'<span style="display:none">{rank:04}</span>' # zero-padded for stable string sort
|
49 |
+
f'<span style="font-size:2.0rem;">{medal}</span>'
|
50 |
+
)
|
51 |
+
# For other ranks, also zero-pad to keep width and ensure proper string sort
|
52 |
+
return f'<span style="display:none">{rank:04}</span>{rank}'
|
53 |
|
54 |
df["Rank"] = df.index + 1
|
55 |
df["Rank"] = df["Rank"].apply(medal_html)
|