Skip to content

unaiverse.stats_html_renderer

This module turns the JSON of a Plotly figure into a standalone HTML page. Stats.plot() uses it for the dashboard of a world. The node of the world sends that page to a person who joins the world and to a member whose statistics request lists no statistic names. The page needs no Python package for plotting because the browser that opens it loads Plotly.js 2.35.2 from https://cdn.plot.ly. The module has one public function.

from unaiverse.stats import UIPlot
from unaiverse.stats_html_renderer import render_plotly_html

panel = UIPlot(title="Members")
panel.add_line([0, 1, 2], [3, 5, 4], name="world agents")
with open("members.html", "w", encoding="utf-8") as f:
    f.write(render_plotly_html(panel.to_json(), title="Members"))

render_plotly_html

render_plotly_html(dash_json: str, title: str = 'World Overview') -> str
Source code in unaiverse/stats_html_renderer.py
def render_plotly_html(dash_json: str, title: str = "World Overview") -> str:
    """Wrap a Plotly-format JSON string in a self-contained HTML document.

    Args:
        dash_json: JSON string with ``'data'`` and ``'layout'`` keys, as produced by
            ``DefaultBaseDash.to_json()`` or ``UIPlot.to_json()``.
        title: Browser tab title for the document.

    Returns:
        A ``<!DOCTYPE html>`` string that renders the chart via Plotly.js CDN.
    """
    return _TEMPLATE.format(title=title, dash_json=dash_json)

Returns a complete HTML5 document as a string. The page has a dark background (#0E0F14) with light text, a single div that fills the whole viewport and a script that draws the figure with Plotly.newPlot in responsive mode.

Argument Type Default Meaning
dash_json str required The figure as a JSON object with the keys data and layout. UIPlot.to_json() and DefaultBaseDash.to_json() produce this form.
title str "World Overview" The title of the browser tab.

Both arguments are inserted into the page verbatim and nothing escapes them. dash_json becomes the right side of a JavaScript assignment and must therefore be a valid JSON object. A string inside it that contains </script> ends the script element early and breaks the page. The function raises no exception of its own and does not validate its input.