Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: use altair_saver for image export #1943

Merged
merged 2 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.5', '3.6', '3.7', '3.8' ]
python-version: [ '3.6', '3.7', '3.8' ]
name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v1
Expand All @@ -24,7 +24,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[dev]
pip install selenium
pip install altair_saver
- name: Test with pytest
run: |
pytest --doctest-modules altair
2 changes: 1 addition & 1 deletion altair/sphinxext/altairgallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def save_example_pngs(examples, image_dir, make_thumbnails=True):
chart.save(image_file)
hashes[filename] = example_hash
except ImportError:
warnings.warn("Could not import selenium: using generic image")
warnings.warn("Unable to save image: using generic image")
create_generic_image(image_file)

with open(hash_file, 'w') as f:
Expand Down
173 changes: 0 additions & 173 deletions altair/utils/headless.py

This file was deleted.

45 changes: 18 additions & 27 deletions altair/utils/mimebundle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import base64

from .headless import compile_spec
from .html import spec_to_html


Expand All @@ -12,13 +9,13 @@ def spec_to_mimebundle(spec, format, mode=None,
"""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 ['png', 'svg', 'vega', 'vega-lite', 'html', 'json']
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 {'png', 'svg', 'vega', 'vega-lite', 'html', 'json'}
format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'}
the file format to be saved.
mode : string {'vega', 'vega-lite'}
The rendering mode.
Expand All @@ -38,9 +35,8 @@ def spec_to_mimebundle(spec, format, mode=None,

Note
----
The png, svg, and vega outputs require the pillow and selenium Python modules
to be installed. Additionally they requires either chromedriver
(if webdriver=='chrome') or geckodriver (if webdriver=='firefox')
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'")
Expand All @@ -49,34 +45,29 @@ def spec_to_mimebundle(spec, format, mode=None,
if vega_version is None:
raise ValueError("Must specify vega_version")
return {'application/vnd.vega.v{}+json'.format(vega_version[0]): spec}
elif format in ['png', 'svg', 'vega']:
render = compile_spec(spec, format=format, mode=mode,
vega_version=vega_version,
vegaembed_version=vegaembed_version,
vegalite_version=vegalite_version, **kwargs)
if format == 'png':
render = base64.b64decode(render.split(',', 1)[1].encode())
return {'image/png': render}
elif format == 'svg':
return {'image/svg+xml': render}
elif format == 'vega':
assert mode == 'vega-lite' # TODO: handle vega->vega conversion more gracefully
return {'application/vnd.vega.v{}+json'.format(vega_version[0]): render}
elif format == 'html':
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}
elif format == 'vega-lite':
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}
elif format == 'json':
if format == 'json':
return {'application/json': spec}
else:
raise ValueError("format must be one of "
"['png', 'svg', 'vega', 'vega-lite', 'html', 'json']")
raise ValueError("format must be one of "
"['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']")
Loading