""" Leaderboard table components for the leaderboard application. """ import streamlit as st from src.data.processors import get_model_type_style, get_rank_style def render_leaderboard_table(display_df, metric_columns, primary_metric): """ Render the custom HTML leaderboard table Args: display_df (pandas.DataFrame): The DataFrame with the display data metric_columns (list): List of metric column names primary_metric (str): The name of the primary metric """ from src.components.header import render_section_header from src.utils.config import metrics_config # Display model ranking header without the box render_section_header("Model Rankings") # Detect if we have multiple metrics (columns with metric prefixes) has_multiple_metrics = any(":" in col for col in metric_columns) # Group columns by metric if multiple metrics are present metric_groups = {} if has_multiple_metrics: # Primary metric columns (no prefix) primary_cols = [col for col in metric_columns if ":" not in col] metric_groups[primary_metric] = primary_cols # Other metrics for col in metric_columns: if ":" in col: prefix, metric_name = col.split(": ", 1) full_metric_name = next((m for m in metrics_config if m.startswith(prefix)), prefix) if full_metric_name not in metric_groups: metric_groups[full_metric_name] = [] metric_groups[full_metric_name].append(col) else: # Single metric metric_groups[primary_metric] = metric_columns # Start building the HTML table structure html_table = """
""" # Add metric headers for each metric group for metric_name, cols in metric_groups.items(): html_table += f'' # Continue the table structure html_table += """ """ # Add individual column headers for all metrics for metric_name, cols in metric_groups.items(): for col in cols: # Extract the actual column name if it has a prefix display_name = col.split(": ", 1)[-1] if ":" in col else col column_class = "overall-cell" if display_name == "Metric Average" else "metric-cell" html_table += f'' # Close the header and start the body html_table += """ """ # Add the data rows for i, (idx, row) in enumerate(display_df.iterrows()): # Define background colors to ensure consistency # Special background for human row is_human_row = row["Agent"] == "Top Human in Competition" if is_human_row: row_bg = "#2a1e37" # Purple-ish dark background for human row row_style = f'style="background-color: {row_bg}; box-shadow: 0 0 5px #f472b6;"' else: row_bg = "#0a0a0a" if i % 2 == 0 else "#111111" row_style = f'style="background-color: {row_bg};"' # Start the row html_table += f'' # Add Rank with medal styling and consistent background rank_style = "" # Don't set background at cell level rank_styles = get_rank_style(row["Rank"]) for style_key, style_value in rank_styles.items(): rank_style += f"{style_key}: {style_value};" html_table += f'' # Model name fixed column with consistent background html_table += f'' # Model type cell model_type = row["Model Type"] type_style = f"text-align: center;" model_type_styles = get_model_type_style(model_type) for style_key, style_value in model_type_styles.items(): if style_value: type_style += f"{style_key}: {style_value};" html_table += f'' # Add metric values with minimal styling for all columns all_metric_columns = [col for group in metric_groups.values() for col in group] for col in all_metric_columns: display_name = col.split(": ", 1)[-1] if ":" in col else col cell_class = "table-cell overall-cell" if display_name == "Metric Average" else "table-cell metric-cell" # Check if column exists in the row (it should) if col in row: value_text = row[col] # Simple styling based on positive/negative values try: value = float(str(row[col]).replace(',', '')) if value > 0: cell_class += " positive-value" elif value < 0: cell_class += " negative-value" except: pass html_table += f'' else: # If column doesn't exist (shouldn't happen), add empty cell html_table += f'' html_table += "" # Close the table html_table += """
Rank Agent Model Type{metric_name}
{display_name}
{row["Rank"]}{row["Agent"]}{model_type}{value_text}-
""" # Add styling for metrics section metrics_css = """ """ # Build a clean HTML string for the metrics section metrics_html = '
' # Add each metric definition for metric_name, metric_info in metrics_config.items(): metric_description = metric_info.get('description', '') # Special handling for Relative Improvement to Human to show formula if metric_name == "Relative Improvement to Human": formula_html = """

Formula:

Relative Improvement to Human =
sagent - sbaseline
stop_human - sbaseline
× 100%

Where:

This metric normalizes scores by setting the baseline solution to 0 and the top human solution to 100.

""" # Add the metric definition with the formula metrics_html += f'

{metric_name}

{metric_description}

{formula_html}
' # Special handling for Absolute Improvement to Baseline to show formula elif metric_name == "Absolute Improvement to Baseline": formula_html = """

Formula:

Absolute Improvement to Baseline =
sagent - sbaseline
sbaseline
× 100%

Where:

This metric measures the percentage improvement of an agent's performance over the baseline solution.

""" # Add the metric definition with the formula metrics_html += f'

{metric_name}

{metric_description}

{formula_html}
' else: # Regular metric without formula metrics_html += f'

{metric_name}

{metric_description}

' # Close the metric definitions container metrics_html += '
' # Display the styling and HTML separately for maximum control st.markdown(html_table, unsafe_allow_html=True) st.markdown(metrics_css, unsafe_allow_html=True) # Render the metrics definitions st.markdown(metrics_html, unsafe_allow_html=True) def render_empty_state(): """ Render an empty state when no data is available """ st.markdown("""
No data to display. Please select at least one task to view the data.
""", unsafe_allow_html=True)