Skip to content

Renderer

HTML renderer for chart.xkcd charts.

render(chart, output_path, chart_js_url, width=600, height=400)

Render a chart to an HTML file.

Parameters:

Name Type Description Default
chart _BaseChart

chart to render.

required
output_path Path | str

where to write result.

required
chart_js_url str

URL to load the chart.xkcd JavaScript module from.

required
width int

chart width in pixels.

600
height int

chart height in pixels.

400
Source code in src/chart_xkcd/renderer.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def render(
    chart: _BaseChart,
    output_path: Path | str,
    chart_js_url: str,
    width: int = 600,
    height: int = 400,
) -> None:
    """Render a chart to an HTML file.

    Args:
        chart: chart to render.
        output_path: where to write result.
        chart_js_url: URL to load the chart.xkcd JavaScript module from.
        width: chart width in pixels.
        height: chart height in pixels.
    """
    Path(output_path).write_text(
        to_html(chart, chart_js_url=chart_js_url, width=width, height=height)
    )

to_html(chart, chart_js_url, width=600, height=400)

Return HTML for a chart as a string.

Parameters:

Name Type Description Default
chart _BaseChart

chart to convert.

required
chart_js_url str

URL to load the chart.xkcd JavaScript module from.

required
width int

chart width in pixels.

600
height int

chart height in pixels.

400

Returns:

Type Description
str

HTML as text.

Source code in src/chart_xkcd/renderer.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def to_html(
    chart: _BaseChart,
    chart_js_url: str,
    width: int = 600,
    height: int = 400,
) -> str:
    """Return HTML for a chart as a string.

    Args:
        chart: chart to convert.
        chart_js_url: URL to load the chart.xkcd JavaScript module from.
        width: chart width in pixels.
        height: chart height in pixels.

    Returns:
        HTML as text.
    """
    chart_type = type(chart).__name__
    config = json.dumps(chart.to_dict(), indent=2)
    return _TEMPLATE.format(
        title=chart.title or "",
        chart_js_url=chart_js_url,
        width=width,
        height=height,
        chart_type=chart_type,
        config=config,
    )