Ryan McConville commited on
Commit
1bacdcc
Β·
1 Parent(s): 23f0448

fix sorting with medal icons

Browse files
Files changed (1) hide show
  1. 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
- m = medal_map.get(rank)
37
- return f'<span style="font-size:2.0rem;">{m}</span>' if m else rank
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)