def test_infer_mode(spec: JSONDict) -> None: mimetype, vg_spec = render(spec, "vega").popitem() assert mimetype == fmt_to_mimetype("vega") mimetype, vl_svg = render(spec, "svg").popitem() assert mimetype == fmt_to_mimetype("svg") mimetype, vg_svg = render(vg_spec, "svg").popitem() assert mimetype == fmt_to_mimetype("svg") assert vl_svg == vg_svg
def test_render_spec(spec: JSONDict) -> None: bundle = render(spec, fmts=FORMATS) assert len(bundle) == len(FORMATS) for mimetype, content in bundle.items(): fmt = mimetype_to_fmt(mimetype) if isinstance(content, dict): check_output(json.dumps(content), fmt) else: check_output(content, fmt)
def test_render_chart(chart: alt.TopLevelMixin) -> None: bundle = render(chart, fmts=FORMATS) assert len(bundle) == len(FORMATS) for mimetype, content in bundle.items(): fmt = mimetype_to_fmt(mimetype) if isinstance(content, dict): check_output(json.dumps(content), fmt) else: check_output(content, fmt)
def test_render_w_vega_cli_options( monkeypatch: MonkeyPatch, capsys: SysCapture, chart: alt.TopLevelMixin, vega_cli_options: Optional[List[str]], ) -> None: """Tests that `vega_cli_options` works with both NodeSaver and other Savers""" monkeypatch.setattr(NodeSaver, "enabled", lambda: True) monkeypatch.setattr(SeleniumSaver, "enabled", lambda: False) bundle = render(chart, fmts=["html", "svg"], vega_cli_options=vega_cli_options) assert len(bundle) == 2 for mimetype, content in bundle.items(): assert content is not None fmt = mimetype_to_fmt(mimetype) if isinstance(content, dict): check_output(json.dumps(content), fmt) else: check_output(content, fmt) stderr = capsys.readouterr().err if vega_cli_options: assert "DEBUG" in stderr
def spec_to_mimebundle( spec, format, mode=None, vega_version=None, vegaembed_version=None, vegalite_version=None, **kwargs, ): """Convert a vega/vega-lite specification to a mimebundle The mimebundle type is controlled by the ``format`` argument, which can be one of the following ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'] Parameters ---------- spec : dict a dictionary representing a vega-lite plot spec format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'} the file format to be saved. mode : string {'vega', 'vega-lite'} The rendering mode. vega_version : string The version of vega.js to use vegaembed_version : string The version of vegaembed.js to use vegalite_version : string The version of vegalite.js to use. Only required if mode=='vega-lite' **kwargs : Additional arguments will be passed to the generating function Returns ------- output : dict a mime-bundle representing the image Note ---- The png, svg, pdf, and vega outputs require the altair_saver package to be installed. """ if mode not in ["vega", "vega-lite"]: raise ValueError("mode must be either 'vega' or 'vega-lite'") if mode == "vega" and format == "vega": if vega_version is None: raise ValueError("Must specify vega_version") return {"application/vnd.vega.v{}+json".format(vega_version[0]): spec} if format in ["png", "svg", "pdf", "vega"]: try: import altair_saver except ImportError: raise ValueError( "Saving charts in {fmt!r} format requires the altair_saver package: " "see http://github.com/altair-viz/altair_saver/".format( fmt=format)) return altair_saver.render(spec, format, mode=mode, **kwargs) if format == "html": html = spec_to_html( spec, mode=mode, vega_version=vega_version, vegaembed_version=vegaembed_version, vegalite_version=vegalite_version, **kwargs, ) return {"text/html": html} if format == "vega-lite": assert mode == "vega-lite" # sanity check: should never be False if mode == "vega": raise ValueError("Cannot convert a vega spec to vegalite") if vegalite_version is None: raise ValueError("Must specify vegalite_version") return { "application/vnd.vegalite.v{}+json".format(vegalite_version[0]): spec } if format == "json": return {"application/json": spec} raise ValueError( "format must be one of " "['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']")
def test_embed_options_render_html(spec: JSONDict, embed_options: JSONDict) -> None: with alt.renderers.set_embed_options(**embed_options): mimetype, html = render(spec, "html").popitem() assert mimetype == "text/html" assert json.dumps(embed_options or {}) in html
def test_infer_mode(spec: JSONDict) -> None: vg_spec = render(spec, "vega").popitem()[1] vl_svg = render(spec, "svg").popitem()[1] vg_svg = render(vg_spec, "svg").popitem()[1] assert vl_svg == vg_svg