File size: 2,303 Bytes
3c19b11
 
 
 
 
 
 
 
d285155
 
3c19b11
 
 
 
 
 
 
 
3874098
 
 
 
 
 
 
 
 
 
3c19b11
 
 
 
 
7990e30
d285155
7990e30
 
3c19b11
 
 
3874098
 
 
 
 
 
 
3c19b11
 
 
3874098
3c19b11
 
 
 
 
 
 
 
78b04f0
3c19b11
 
 
 
3874098
3c19b11
3874098
3c19b11
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Display helpers for Gradio tables."""

from __future__ import annotations

import html

import pandas as pd

from src.formatting import fmt_number


def display_dataframe(df: pd.DataFrame | None) -> pd.DataFrame:
    if df is None or df.empty:
        return pd.DataFrame([{"β€”": "No data"}])
    out = df.copy().astype(object)
    return out.where(pd.notnull(out), "β€”")


def _header_cell(col: object, *, wrap: bool) -> str:
    text = str(col)
    if wrap and " " in text:
        left, right = text.split(" ", 1)
        inner = f"{html.escape(left)}<br>{html.escape(right)}"
    else:
        inner = html.escape(text)
    return f"<th>{inner}</th>"


def _cell_text(value: object) -> str:
    if value is None or (isinstance(value, float) and pd.isna(value)):
        return "β€”"
    if pd.isna(value):
        return "β€”"
    if isinstance(value, float):
        return fmt_number(value)
    if isinstance(value, int) and not isinstance(value, bool):
        return str(value)
    return str(value)


def dataframe_to_html(
    df: pd.DataFrame | None,
    *,
    empty_note: str = "No data",
    wrap_headers: bool = False,
    table_class: str = "",
) -> str:
    if df is None or df.empty:
        return f'<p class="wab-detail-note">{html.escape(empty_note)}</p>'

    headers = "".join(_header_cell(col, wrap=wrap_headers) for col in df.columns)
    body_rows = []
    for row in df.itertuples(index=False, name=None):
        cells = []
        for col, value in zip(df.columns, row, strict=True):
            text = _cell_text(value)
            css_class = ""
            if col in {"Success %", "Success"} and text not in {"β€”", "βœ“", "βœ—"}:
                css_class = ' class="wab-metric-value"'
            elif col in {"Tasks", "Metric", "Harness", "Input", "Model", "Class", "Task"}:
                css_class = ' class="wab-mono-cell"'
            cells.append(f"<td{css_class}>{html.escape(text)}</td>")
        body_rows.append(f"<tr>{''.join(cells)}</tr>")
    rows = "".join(body_rows)
    wrap_cls = f" {table_class}" if table_class else ""
    return (
        f'<div class="wab-table-wrap wab-detail-table-wrap{wrap_cls}">'
        f'<table class="wab-table wab-detail-table"><thead><tr>{headers}</tr></thead>'
        f"<tbody>{rows}</tbody></table></div>"
    )