import json from typing import Optional, Dict import jinja2 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[str, jinja2.Template] = { "standard": HTML_TEMPLATE, "universal": HTML_TEMPLATE_UNIVERSAL, "inline": INLINE_HTML_TEMPLATE, } def spec_to_html( spec: dict, mode: str, vega_version: str, vegaembed_version: str, vegalite_version: Optional[str] = None, base_url: str = "https://cdn.jsdelivr.net/npm", output_div: str = "vis", embed_options: Optional[dict] = None, json_kwds: Optional[dict] = None, fullhtml: bool = True, requirejs: bool = False, template: str = "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