Skip to content

Report Export

reliably.report.render.to_html(report, path=None)

Render a reliability report to HTML using Jinja2.

Parameters:

Name Type Description Default
report Report

The report to render.

required
path str | Path | None

If given, write the HTML to this file.

None

Returns:

Type Description
str

HTML string.

Examples:

>>> from reliably._core.results import Report
>>> r = Report(task="binary", metrics={}, n=100, meta={})
>>> html = to_html(r)
>>> "<html" in html
True
Source code in src/reliably/report/render.py
def to_html(report: Report, path: str | Path | None = None) -> str:
    """Render a reliability report to HTML using Jinja2.

    Parameters
    ----------
    report : Report
        The report to render.
    path : str | Path | None
        If given, write the HTML to this file.

    Returns
    -------
    str
        HTML string.

    Examples
    --------
    >>> from reliably._core.results import Report
    >>> r = Report(task="binary", metrics={}, n=100, meta={})
    >>> html = to_html(r)
    >>> "<html" in html
    True
    """
    try:
        from jinja2 import Environment, PackageLoader, select_autoescape
    except ImportError as exc:
        raise ImportError(
            "jinja2 is required for HTML reports. "
            "Install with: pip install reliably[report]"
        ) from exc

    env = Environment(
        loader=PackageLoader("reliably.report", "templates"),
        autoescape=select_autoescape(["html"]),
    )
    template = env.get_template("card.html.j2")
    html: str = template.render(report=report, version=_VERSION)

    if path is not None:
        Path(path).write_text(html, encoding="utf-8")
    return html

reliably.report.render.to_markdown(report)

Render a reliability report to a Markdown table.

Parameters:

Name Type Description Default
report Report

The report to render.

required

Returns:

Type Description
str

Markdown string.

Examples:

>>> from reliably._core.results import Report
>>> r = Report(task="binary", metrics={}, n=100, meta={})
>>> md = to_markdown(r)
>>> "| Metric" in md
True
Source code in src/reliably/report/render.py
def to_markdown(report: Report) -> str:
    """Render a reliability report to a Markdown table.

    Parameters
    ----------
    report : Report
        The report to render.

    Returns
    -------
    str
        Markdown string.

    Examples
    --------
    >>> from reliably._core.results import Report
    >>> r = Report(task="binary", metrics={}, n=100, meta={})
    >>> md = to_markdown(r)
    >>> "| Metric" in md
    True
    """
    lines = [
        "# Reliability Report",
        "",
        f"**Task:** {report.task}  **N:** {report.n}",
        "",
        "| Metric | Value | 95% CI | n |",
        "|--------|-------|--------|---|",
    ]
    for name, mr in report.metrics.items():
        ci_str = (
            f"[{mr.ci.low:.4f}, {mr.ci.high:.4f}]" if mr.ci else "—"
        )
        lines.append(f"| {mr.name} | {mr.value:.4f} | {ci_str} | {mr.n} |")
        if mr.extra:
            for k, v in mr.extra.items():
                lines.append(f"| &nbsp;&nbsp;{k} | {v:.4f} | | |")
    lines.append("")
    lines.append(f"*Generated by reliably v{_VERSION}*")
    return "\n".join(lines)