diff --git a/altair/utils/_importers.py b/altair/utils/_importers.py index 388d379f5..bb539fb73 100644 --- a/altair/utils/_importers.py +++ b/altair/utils/_importers.py @@ -29,7 +29,7 @@ def import_vegafusion() -> ModuleType: def import_vl_convert() -> ModuleType: - min_version = "1.0.0" + min_version = "1.0.1" try: version = importlib_version("vl-convert-python") if Version(version) < Version(min_version): @@ -42,7 +42,7 @@ def import_vl_convert() -> ModuleType: return vlc except ImportError as err: raise ImportError( - f"The vl-convert Vega-Lite compiler and image export feature requires\n" + f"The vl-convert Vega-Lite compiler and file export feature requires\n" f"version {min_version} or greater of the 'vl-convert-python' package. \n" f"This can be installed with pip using:\n" f' pip install "vl-convert-python>={min_version}"\n' @@ -52,6 +52,14 @@ def import_vl_convert() -> ModuleType: ) from err +def vl_version_for_vl_convert() -> str: + from ..vegalite import SCHEMA_VERSION + + # Compute VlConvert's vl_version string (of the form 'v5_2') + # from SCHEMA_VERSION (of the form 'v5.2.0') + return "_".join(SCHEMA_VERSION.split(".")[:2]) + + def import_pyarrow_interchange() -> ModuleType: min_version = "11.0.0" try: diff --git a/altair/utils/html.py b/altair/utils/html.py index b2aeb5514..6cd89b2dd 100644 --- a/altair/utils/html.py +++ b/altair/utils/html.py @@ -3,6 +3,7 @@ import jinja2 +from altair.utils._importers import import_vl_convert, vl_version_for_vl_convert HTML_TEMPLATE = jinja2.Template( """ @@ -182,11 +183,7 @@ } @@ -256,7 +253,7 @@ def spec_to_html( tags. If True, then load libraries using requirejs template : jinja2.Template or string (optional) Specify the template to use (default = 'standard'). If template is a - string, it must be one of {'universal', 'standard'}. Otherwise, it + string, it must be one of {'universal', 'standard', 'inline'}. Otherwise, it can be a jinja2.Template object containing a custom template. Returns @@ -283,19 +280,9 @@ def spec_to_html( render_kwargs = {} if template == "inline": - try: - from altair_viewer import get_bundled_script - except ImportError as err: - raise ImportError( - "The altair_viewer package is required to convert to HTML with inline=True" - ) from err - render_kwargs["vega_script"] = get_bundled_script("vega", vega_version) - render_kwargs["vegalite_script"] = get_bundled_script( - "vega-lite", vegalite_version - ) - render_kwargs["vegaembed_script"] = get_bundled_script( - "vega-embed", vegaembed_version - ) + vlc = import_vl_convert() + vl_version = vl_version_for_vl_convert() + render_kwargs["vegaembed_script"] = vlc.javascript_bundle(vl_version=vl_version) jinja_template = TEMPLATES.get(template, template) if not hasattr(jinja_template, "render"): diff --git a/altair/utils/mimebundle.py b/altair/utils/mimebundle.py index ac390f02c..678636727 100644 --- a/altair/utils/mimebundle.py +++ b/altair/utils/mimebundle.py @@ -2,7 +2,7 @@ from .deprecation import AltairDeprecationWarning from .html import spec_to_html -from ._importers import import_vl_convert +from ._importers import import_vl_convert, vl_version_for_vl_convert import struct import warnings @@ -116,11 +116,7 @@ def _spec_to_mimebundle_with_engine( if normalized_engine == "vlconvert": vlc = import_vl_convert() - from ..vegalite import SCHEMA_VERSION - - # Compute VlConvert's vl_version string (of the form 'v5_2') - # from SCHEMA_VERSION (of the form 'v5.2.0') - vl_version = "_".join(SCHEMA_VERSION.split(".")[:2]) + vl_version = vl_version_for_vl_convert() if format == "vega": if mode == "vega": vg = spec diff --git a/altair/utils/save.py b/altair/utils/save.py index 97f86c710..fd8cc2b63 100644 --- a/altair/utils/save.py +++ b/altair/utils/save.py @@ -114,7 +114,7 @@ def save( Additional keyword arguments are passed to the output method associated with the specified format. webdriver : string {'chrome' | 'firefox'} (optional) - Webdriver to use for png or svg output + Webdriver to use for png, svg, or pdf output when using altair_saver engine scale_factor : float (optional) scale_factor to use to change size/resolution of png or svg output engine: string {'vl-convert', 'altair_saver'} @@ -124,7 +124,7 @@ def save( from a CDN location in the resulting html file. If True, the required JavaScript libraries are inlined into the resulting html file so that it will work without an internet connection. - The altair_viewer package is required if True. + The vl-convert-python package is required if True. **kwargs : additional kwargs passed to spec_to_mimebundle. """ diff --git a/altair/vegalite/v5/api.py b/altair/vegalite/v5/api.py index f0126ac38..56c540426 100644 --- a/altair/vegalite/v5/api.py +++ b/altair/vegalite/v5/api.py @@ -1040,7 +1040,43 @@ def to_html( json_kwds: Optional[dict] = None, fullhtml: bool = True, requirejs: bool = False, + inline: bool = False, + **kwargs, ) -> str: + """Embed a Vega/Vega-Lite spec into an HTML page + + Parameters + ---------- + 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