"""HTML export for visual inspection of MathVision-like records.""" from __future__ import annotations from html import escape from pathlib import Path from mathvision_explorer.dataset import MathVisionRecord def export_html(records: list[MathVisionRecord], output: Path) -> None: """Write a standalone HTML gallery for records.""" output.parent.mkdir(parents=True, exist_ok=True) cards = "\n".join(_render_card(record, output_dir=output.parent) for record in records) html = f""" MathVision Explorer

MathVision Explorer

{len(records)} visual math records
{cards}
""" output.write_text(html, encoding="utf-8") def _render_card(record: MathVisionRecord, *, output_dir: Path) -> str: image_html = "" if record.image_path is not None: image_src = _relative_or_absolute_image(record.image_path, output_dir=output_dir) image_html = f' {escape(record.problem_id)}\n' meta = [_tag(record.problem_id)] if record.subject is not None: meta.append(_tag(record.subject)) if record.level is not None: meta.append(_tag(f"level {record.level}")) options = "" if record.options: options = f"

Options: {escape(', '.join(record.options))}

" solution = "" if record.solution: solution = f"

{escape(record.solution)}

" return f"""
{image_html}
{''.join(meta)}

{escape(record.question)}

Answer: {escape(record.answer)}

{options} {solution}
""" def _tag(value: str) -> str: return f'{escape(value)}' def _relative_or_absolute_image(image_path: Path, *, output_dir: Path) -> str: try: return image_path.resolve().relative_to(output_dir.resolve()).as_posix() except ValueError: return image_path.resolve().as_uri()