Skip to content

vega

vega #

embed — the one way a component draws a Vega-Lite specification into a page.

SCRIPTS = ((VEGA, 'vega.min.js'), (VEGA_LITE, 'vega-lite.min.js'), (VEGA_EMBED, 'vega-embed.min.js')) module-attribute #

The browser libraries a drawn specification needs, in load order.

embed(render, spec) #

The library scripts (once per page), a container, and the vegaEmbed call for spec.

return embed(render, {"mark": "bar", "data": {"values": [{"n": 1}]}})
Source code in capturegraph-ui/capturegraph_ui/charts/vega.py
def embed(render: Render, spec: dict[str, cg.JSONValue]) -> str:
    """The library scripts (once per page), a container, and the ``vegaEmbed`` call for ``spec``.

    ```python
    return embed(render, {"mark": "bar", "data": {"values": [{"n": 1}]}})
    ```
    """
    identifier = render.identifier("chart")
    scripts = "".join(
        render.once(library.name, element("script", {"src": render.lib(library, file)}))
        for library, file in SCRIPTS
    )
    return (
        scripts
        + element("div", {"class": "cg-chart", "id": identifier})
        + element(
            "script",
            None,
            f"vegaEmbed({inline_json('#' + identifier)}, {inline_json(spec)}, "
            f'{{"actions": false}});',
        )
    )