Skip to content

Commit

Permalink
ENH: add support for built-in vega themes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakevdp committed May 30, 2019
1 parent 2e74eb4 commit b46b699
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions altair/vegalite/v3/tests/test_theme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

import altair.vegalite.v3 as alt
from altair.vegalite.v3.theme import VEGA_THEMES


@pytest.fixture
def chart():
return alt.Chart('data.csv').mark_bar().encode(x='x:Q')

def test_vega_themes(chart):
for theme in VEGA_THEMES:
with alt.themes.enable(theme):
dct = chart.to_dict()
assert dct['usermeta'] == {'embedOptions': {'theme': theme}}
assert dct['config'] == {"view": {"width": 400, "height": 300},
"mark": {"tooltip": None}}
21 changes: 21 additions & 0 deletions altair/vegalite/v3/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

from ...utils.theme import ThemeRegistry

VEGA_THEMES = ['ggplot2', 'quartz', 'vox', 'fivethirtyeight', 'dark', 'latimes']


class VegaTheme(object):
"""Implementation of a builtin vega theme."""
def __init__(self, theme):
self.theme = theme

def __call__(self):
return {"usermeta": {"embedOptions": {"theme": self.theme}},
"config": {"view": {"width": 400, "height": 300},
"mark": {"tooltip": None}}}

def __repr__(self):
return "VegaTheme({!r})".format(self.theme)


# The entry point group that can be used by other packages to declare other
# renderers that will be auto-detected. Explicit registration is also
# allowed by the PluginRegistery API.
Expand All @@ -14,4 +31,8 @@
"view": {"width": 400, "height": 300},
"mark": {"tooltip": None}}})
themes.register('none', lambda: {})

for theme in VEGA_THEMES:
themes.register(theme, VegaTheme(theme))

themes.enable('default')

0 comments on commit b46b699

Please sign in to comment.