from __future__ import annotations import json from typing import Any, Literal import jinja2 from altair.utils._importers import import_vl_convert, vl_version_for_vl_convert TemplateName = Literal["standard", "universal", "inline"] RenderMode = Literal["vega", "vega-lite"] HTML_TEMPLATE = jinja2.Template( """ {%- if fullhtml -%}
{%- endif %} {%- if not requirejs %} {%- if mode == 'vega-lite' %} {%- endif %} {%- endif %} {%- if fullhtml %} {%- if requirejs %} {%- endif %} {%- endif %} {%- if fullhtml %} {%- endif %} """ ) HTML_TEMPLATE_UNIVERSAL = jinja2.Template( """ """ ) # This is like the HTML_TEMPLATE template, but includes vega javascript inline # so that the resulting file is not dependent on external resources. This was # ported over from altair_saver. # # implies requirejs=False and full_html=True INLINE_HTML_TEMPLATE = jinja2.Template( """\ """ ) TEMPLATES: dict[TemplateName, jinja2.Template] = { "standard": HTML_TEMPLATE, "universal": HTML_TEMPLATE_UNIVERSAL, "inline": INLINE_HTML_TEMPLATE, } def spec_to_html( spec: dict[str, Any], mode: RenderMode, vega_version: str | None, vegaembed_version: str | None, vegalite_version: str | None = None, base_url: str = "https://cdn.jsdelivr.net/npm", output_div: str = "vis", embed_options: dict[str, Any] | None = None, json_kwds: dict[str, Any] | None = None, fullhtml: bool = True, requirejs: bool = False, template: jinja2.Template | TemplateName = "standard", ) -> str: """ Embed a Vega/Vega-Lite spec into an HTML page. Parameters ---------- spec : dict a dictionary representing a vega-lite plot spec. mode : string {'vega' | 'vega-lite'} The rendering mode. This value is overridden by embed_options['mode'], if it is present. vega_version : string For html output, the version of vega.js to use. vegalite_version : string For html output, the version of vegalite.js to use. vegaembed_version : string For html output, the version of vegaembed.js to use. base_url : string (optional) The base url from which to load the javascript libraries. output_div : string (optional) The id of the div element where the plot will be shown. embed_options : dict (optional) Dictionary of options to pass to the vega-embed script. Default entry is {'mode': mode}. json_kwds : dict (optional) Dictionary of keywords to pass to json.dumps(). fullhtml : boolean (optional) If True (default) then return a full html page. If False, then return an HTML snippet that can be embedded into an HTML page. requirejs : boolean (optional) If False (default) then load libraries from base_url using