From 2969c25192001759c85311de4b842dcfb6e203e5 Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Fri, 31 Aug 2018 09:11:05 +0100 Subject: [PATCH 1/6] MAINT: move write_file_or_filename util --- altair/utils/__init__.py | 3 +-- altair/utils/core.py | 9 --------- altair/utils/save.py | 10 +++++++++- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/altair/utils/__init__.py b/altair/utils/__init__.py index a1c2a38c4..841484e8f 100644 --- a/altair/utils/__init__.py +++ b/altair/utils/__init__.py @@ -5,11 +5,11 @@ use_signature, update_subtraits, update_nested, - write_file_or_filename, display_traceback, SchemaBase, Undefined ) +from .html import spec_to_html from .plugin_registry import PluginRegistry @@ -20,7 +20,6 @@ 'use_signature', 'update_subtraits', 'update_nested', - 'write_file_or_filename', 'display_traceback', 'SchemaBase', 'Undefined', diff --git a/altair/utils/core.py b/altair/utils/core.py index 51c16589e..a0d3d8d56 100644 --- a/altair/utils/core.py +++ b/altair/utils/core.py @@ -386,15 +386,6 @@ def update_nested(original, update, copy=False): return original -def write_file_or_filename(fp, content, mode='w'): - """Write content to fp, whether fp is a string or a file-like object""" - if isinstance(fp, six.string_types): - with open(fp, mode) as f: - f.write(content) - else: - fp.write(content) - - def display_traceback(in_ipython=True): exc_info = sys.exc_info() diff --git a/altair/utils/save.py b/altair/utils/save.py index 814190f31..6ffa46686 100644 --- a/altair/utils/save.py +++ b/altair/utils/save.py @@ -2,10 +2,18 @@ import six -from .core import write_file_or_filename from .mimebundle import spec_to_mimebundle +def write_file_or_filename(fp, content, mode='w'): + """Write content to fp, whether fp is a string or a file-like object""" + if isinstance(fp, six.string_types): + with open(fp, mode) as f: + f.write(content) + else: + fp.write(content) + + def save(chart, fp, vega_version, vegaembed_version, format=None, mode=None, vegalite_version=None, embed_options=None, json_kwds=None, webdriver='chrome', From 5f8470e159ea1b5dc7847d80e774a5ac6a08f332 Mon Sep 17 00:00:00 2001 From: Jake VanderPlas Date: Fri, 31 Aug 2018 09:34:35 +0100 Subject: [PATCH 2/6] MAINT: add flexibility to HTML output --- altair/utils/html.py | 133 +++++++++++++-------- altair/vegalite/v2/api.py | 11 ++ altair/vegalite/v2/tests/test_api.py | 2 +- altair/vegalite/v2/tests/test_renderers.py | 8 +- requirements.txt | 1 + requirements_dev.txt | 1 - 6 files changed, 99 insertions(+), 57 deletions(-) diff --git a/altair/utils/html.py b/altair/utils/html.py index 1afd22d44..37831ee03 100644 --- a/altair/utils/html.py +++ b/altair/utils/html.py @@ -1,76 +1,103 @@ from __future__ import unicode_literals import json +import jinja2 -TOP = """ +HTML_TEMPLATE = jinja2.Template(""" +{%- if fullhtml -%} +{%- endif %} -""" - -VEGA_SCRIPTS = """ - - -""" - -VEGALITE_SCRIPTS = """ - - - -""" - -BOTTOM = """ +{%- if not requirejs %} + + {%- if mode == 'vega-lite' %} + + {%- endif %} + +{%- endif %} +{%- if fullhtml %} +{%- if requirejs %} + + +{%- endif %} -
- +{%- if fullhtml %} +{%- endif %} """ - - -HTML_TEMPLATE = { - 'vega-lite': TOP + VEGALITE_SCRIPTS + BOTTOM, - 'vega': TOP + VEGA_SCRIPTS + BOTTOM -} +) def spec_to_html(spec, mode, vega_version, vegaembed_version, vegalite_version=None, base_url="https://cdn.jsdelivr.net/npm/", - output_div='vis', embed_options=None, json_kwds=None): + output_div='vis', embed_options=None, json_kwds=None, + fullhtml=True, requirejs=False): """Embed a Vega/Vega-Lite spec into an HTML page Parameters ---------- spec : dict a dictionary representing a vega-lite plot spec. + fullhtml : boo mode : string {'vega' | 'vega-lite'} The rendering mode. This value is overridden by embed_options['mode'], if it is present. @@ -88,11 +115,17 @@ def spec_to_html(spec, mode, Dictionary of options to pass to the vega-embed script. 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 ") + + if requirejs: + assert "require(" in html + else: + assert "require(" not in html + + assert "vega-lite@{}".format(vegalite_version) in html + assert "vega@{}".format(vega_version) in html + assert "vega-embed@{}".format(vegaembed_version) in html