File size: 2,272 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pydantic import BaseModel, Field

from app.agents.d3.registry import D3Template, register


class DataTableRow(BaseModel):
    label: str
    values: list[str]


class DataTableData(BaseModel):
    title: str = ""
    columns: list[str] = Field(min_length=1, max_length=16)
    rows: list[DataTableRow] = Field(min_length=1, max_length=100)


_HTML = """<!DOCTYPE html><html><head><meta charset="utf-8"><script src="https://d3js.org/d3.v7.min.js"></script><style>
html,body{margin:0;width:100%;height:100%;background:#FAF7F2;color:#1A3557;font-family:Inter,system-ui,sans-serif}*{box-sizing:border-box}#root{height:100%;padding:24px;display:flex;flex-direction:column;gap:16px}.title{font:700 20px Georgia,'Libre Caslon Text',serif}.wrap{overflow:auto;border:1px solid #CCD8E3;border-radius:10px;background:#fff}table{width:100%;border-collapse:separate;border-spacing:0;font-size:13px}th{position:sticky;top:0;background:#EAF1F7;text-align:left;font-weight:700;z-index:1}th,td{padding:11px 14px;border-bottom:1px solid #E2E9EF;white-space:nowrap}tbody tr:hover{background:#F5F8FB}td:not(:first-child),th:not(:first-child){text-align:right;font-variant-numeric:tabular-nums}tbody tr:last-child td{border-bottom:0}
</style></head><body><div id="root"><div class="title"></div><div class="wrap"><table><thead><tr></tr></thead><tbody></tbody></table></div></div><script>
const spec=__DATA__,root=d3.select('#root');root.select('.title').text(spec.title);root.select('thead tr').selectAll('th').data(['Item',...spec.columns]).join('th').text(d=>d);const rows=root.select('tbody').selectAll('tr').data(spec.rows).join('tr');rows.selectAll('td').data(d=>[d.label,...d.values]).join('td').text(d=>d);
</script></body></html>"""


register(D3Template(id="data_table", family="chart", title="Compact data table", when_to_use="Preserve selected tabular values in a clean readable table without changing their visual encoding.", data_requirements="Column labels and source-faithful row values.", schema=DataTableData, html_template=_HTML, golden_sample=DataTableData(title="Model comparison", columns=["Math Avg", "STEM"], rows=[DataTableRow(label="Qwen", values=["47.4", "25.2"]), DataTableRow(label="DeepSeek", values=["26.6", "23.2"])])))