from html import escape
def section_header_html(kicker: str, title: str, subtitle: str, *, class_name: str = "bn-panel") -> str:
return (
f"
"
f"
{escape(kicker)}
"
f"
{escape(title)}
"
f"
{escape(subtitle)}
"
"
"
)
def status_pill_html(label: str, value: str, tone: str = "") -> str:
tone_class = f" bn-pill-{tone}" if tone else ""
return f"{escape(label)}: {escape(value)}"
def project_overview_html(project_rows: list[dict], authorized_projects: list[str], selected_project: str | None = None) -> str:
if not authorized_projects:
return (
""
"
No projects assigned
"
"
Create a project in Admin or accept a pending invite to start validating.
"
"
"
)
def normalize_project_row(row: object) -> dict:
if isinstance(row, dict):
return row
if isinstance(row, (list, tuple)):
return {
"project_slug": row[0] if len(row) > 0 else "",
"name": row[1] if len(row) > 1 else "",
"dataset_repo_id": row[2] if len(row) > 2 else "",
"visibility": row[3] if len(row) > 3 else "collaborative",
"owner_username": row[4] if len(row) > 4 else "",
"dataset_token_set": str(row[5]).strip().lower() == "yes" if len(row) > 5 else False,
"active": str(row[6]).strip().lower() == "yes" if len(row) > 6 else True,
}
return {}
normalized_project_rows = [normalize_project_row(row) for row in project_rows]
rows_by_slug = {str(row.get("project_slug", "")): row for row in normalized_project_rows}
cards: list[str] = []
for slug in authorized_projects:
row = rows_by_slug.get(slug, {})
name = str(row.get("name") or slug)
dataset_repo = str(row.get("dataset_repo_id") or "dataset not set")
visibility = str(row.get("visibility") or "collaborative")
active = "active" if bool(row.get("active", True)) else "inactive"
token = "token set" if bool(row.get("dataset_token_set", False)) else "public/default token"
selected_class = " bn-project-card-selected" if selected_project == slug else ""
cards.append(
f""
f"
{escape(name)}
"
f"
{escape(slug)}
"
f"
{escape(dataset_repo)}
"
"
"
f"{status_pill_html('visibility', visibility)}"
f"{status_pill_html('status', active, 'ok' if active == 'active' else 'warn')}"
f"{status_pill_html('access', token)}"
"
"
"
"
)
return "" + "".join(cards) + "
"
def compact_metric_grid(items: list[tuple[str, str, str, str]]) -> str:
cards = []
for label, value, hint, tone in items:
tone_class = f" bn-kpi-{tone}" if tone else ""
cards.append(
f""
f"
{escape(label)}
"
f"
{escape(value)}
"
f"
{escape(hint)}
"
"
"
)
return "" + "".join(cards) + "
"
def admin_overview_html(
*,
username: str | None,
total_projects: int,
admin_projects: int,
validator_projects: int,
pending_invites: int,
) -> str:
if not username:
return (
""
"
Admin workspace locked
"
"
Login to manage projects, teams, invites, and dataset tokens.
"
"
"
)
return compact_metric_grid(
[
("User", username, "active session", "info"),
("Projects", str(total_projects), "registered in app", ""),
("Admin scope", str(admin_projects), "projects you manage", "positive" if admin_projects else "warning"),
("Validator scope", str(validator_projects), f"{pending_invites} pending invites", "info"),
]
)
def project_context_html(project_row: list[object] | None, role: str | None = None) -> str:
if not project_row:
return (
""
"
No project selected
"
"
Choose an authorized project before opening the validation workbench.
"
"
"
)
def cell(index: int, default: str = "") -> str:
if len(project_row) <= index:
return default
return str(project_row[index] or "").strip() or default
slug = cell(0, "unknown-project")
name = cell(1, slug)
dataset_repo = cell(2, "dataset not set")
visibility = cell(3, "collaborative")
owner = cell(4, "unknown")
token = "token set" if cell(5, "no").lower() == "yes" else "public/default token"
active = cell(6, "yes")
role_label = (role or "unknown").upper()
role_tone = "ok" if role_label == "ADMIN" else "info"
active_tone = "ok" if active.lower() == "yes" else "warn"
return (
""
"
"
"
Selected project
"
f"
{escape(name)}
"
f"
{escape(slug)} · {escape(dataset_repo)}
"
"
"
"
"
f"{status_pill_html('role', role_label, role_tone)}"
f"{status_pill_html('visibility', visibility)}"
f"{status_pill_html('owner', owner)}"
f"{status_pill_html('token', token)}"
f"{status_pill_html('active', active, active_tone)}"
"
"
"
"
)
def invite_panel_html(invite_count: int) -> str:
if invite_count <= 0:
return inline_hint_html("No pending invites for this user.", "info")
return (
""
f"
{invite_count} pending invite{'s' if invite_count != 1 else ''}
"
"
Review and accept only the project access you expect to use.
"
"
"
)
def coverage_bars_html(rows: list[list[object]], *, limit: int = 12) -> str:
if not rows:
return (
""
"
Coverage by species0 species
"
"
Refresh the dashboard after selecting a project.
"
"
"
)
cards: list[str] = []
for row in rows[:limit]:
species = str(row[0] if len(row) > 0 else "Unknown species")
total = int(row[1] or 0) if len(row) > 1 else 0
validated = int(row[2] or 0) if len(row) > 2 else 0
remaining = int(row[3] or 0) if len(row) > 3 else max(0, total - validated)
try:
pct = float(row[4] if len(row) > 4 else 0.0)
except Exception:
pct = 0.0
pct = max(0.0, min(100.0, pct))
cards.append(
""
"
"
f"{escape(species)}"
f"{pct:.1f}%"
"
"
"
"
f"
{validated} validated · {remaining} remaining · {total} total
"
"
"
)
remaining_species = max(0, len(rows) - limit)
footer = f"" if remaining_species else ""
return (
""
"
"
f"Coverage by species{len(rows)} species"
"
"
+ "".join(cards)
+ footer
+ "
"
)
def paged_activity_html(
title: str,
headers: list[str],
rows: list[list[object]],
*,
page: int = 1,
page_size: int = 10,
empty_message: str = "No activity yet.",
) -> str:
safe_page_size = max(1, int(page_size))
total_rows = len(rows)
total_pages = max(1, ((total_rows - 1) // safe_page_size) + 1) if total_rows else 1
safe_page = max(1, min(int(page), total_pages))
start = (safe_page - 1) * safe_page_size
page_rows = rows[start : start + safe_page_size]
header_html = "".join(f"{escape(str(header))} | " for header in headers)
if page_rows:
body_html = "".join(
"" + "".join(f"| {escape(str(value or ''))} | " for value in row) + "
"
for row in page_rows
)
else:
body_html = (
"| {escape(empty_message)} |
"
)
return (
""
"
"
f"{escape(title)}{total_rows} records"
"
"
"
"
f"{header_html}
{body_html}"
"
"
f""
"
"
)
def settings_health_html(items: list[tuple[str, str, str]]) -> str:
cards = []
for label, value, tone in items:
tone_class = f" bn-pill-{tone}" if tone else ""
cards.append(
""
f"
{escape(label)}
{escape(value)}
"
f"
{escape(tone or 'info')}"
"
"
)
return "" + "".join(cards) + "
"
def inline_hint_html(text: str, tone: str = "info") -> str:
tone_class = f" bn-hint-{tone}" if tone else ""
return f"{escape(text)}
"
def selected_segment_html(
row: list[object] | None,
selected_index: int | None = None,
total_rows: int | None = None,
empty_title: str = "No segment loaded",
empty_note: str = "Choose a species and apply filters to load the validation queue.",
) -> str:
if not row:
return (
""
"
Selected segment
"
f"
{escape(empty_title)}
"
f"
{escape(empty_note)}
"
"
"
)
def cell(index: int, default: str = "") -> str:
if len(row) <= index:
return default
value = str(row[index] or "").strip()
if index == 2 and value.startswith("▶ "):
value = value[2:].strip()
return value or default
detection_key = cell(0, "unknown")
audio_id = cell(1, "unknown audio")
species = cell(2, "Unknown species")
confidence = cell(3, "0")
start_time = cell(4, "0")
end_time = cell(5, "0")
status = cell(6, "pending")
version = cell(7, "0")
conflict = cell(8, "")
position = ""
if selected_index is not None and total_rows:
position = f"{int(selected_index) + 1}/{int(total_rows)}"
conflict_badge = "conflict" if conflict else ""
return (
""
"
"
"
"
"
Selected segment
"
f"
{escape(species)}
"
"
"
f"
{escape(position or 'queue')}"
"
"
"
"
f"Confidence{escape(confidence)}"
f"Time{escape(start_time)}-{escape(end_time)}s"
f"Status{escape(status)}"
f"Version{escape(version)}"
"
"
f"
{escape(audio_id)}
"
"
"
f"key: {escape(detection_key)}"
f"{conflict_badge}"
"
"
"
"
)
def validation_queue_html(rows: object, selected_index: int | None = None, *, limit: int = 8) -> str:
if hasattr(rows, "values"):
normalized_rows = [list(item) for item in rows.values.tolist()]
else:
normalized_rows = [list(item) for item in rows] if rows else []
if not normalized_rows:
return (
""
"
"
"Queue preview0 loaded"
"
"
"
Select a species and apply filters to load segments.
"
"
"
)
safe_selected = max(0, min(int(selected_index or 0), len(normalized_rows) - 1))
cards: list[str] = []
for idx, row in enumerate(normalized_rows[:limit]):
def cell(index: int, default: str = "") -> str:
if len(row) <= index:
return default
value = str(row[index] or "").strip()
if index == 2 and value.startswith("▶ "):
value = value[2:].strip()
return value or default
species = cell(2, "Unknown species")
confidence = cell(3, "0")
status = cell(6, "pending")
audio_id = cell(1, "unknown audio")
selected_class = " bn-queue-card-selected" if idx == safe_selected else ""
cards.append(
f""
f"
{idx + 1}
"
"
"
f"
{escape(species)}
"
f"
{escape(audio_id)}
"
"
"
"
"
f"{escape(confidence)}"
f"{escape(status)}"
"
"
"
"
)
remaining = max(0, len(normalized_rows) - limit)
remaining_text = f"+{remaining} more on this page" if remaining else "all loaded rows shown"
return (
""
"
"
f"Queue preview{safe_selected + 1}/{len(normalized_rows)}"
"
"
+ "".join(cards)
+ f""
"
"
)