Skip to content

Commit

Permalink
Merge pull request vega#756 from jakevdp/rename-theme
Browse files Browse the repository at this point in the history
Rename alt.theme to alt.themes for consistency
  • Loading branch information
jakevdp authored Apr 13, 2018
2 parents 7276237 + b78776e commit 83291b5
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 31 deletions.
23 changes: 16 additions & 7 deletions altair/sphinxext/altairgallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class AltairMiniGalleryDirective(Directive):
has_content = False

option_spec = {'size': int,
'names': str,
'indices': lambda x: list(map(int, x.split())),
'shuffle': flag,
'seed': int,
Expand All @@ -168,6 +169,7 @@ class AltairMiniGalleryDirective(Directive):

def run(self):
size = self.options.get('size', 15)
names = [name.strip() for name in self.options.get('names', '').split(',')]
indices = self.options.get('indices', [])
shuffle = 'shuffle' in self.options
seed = self.options.get('seed', 42)
Expand All @@ -181,13 +183,20 @@ def run(self):

examples = populate_examples()

if indices:
examples = [examples[i] for i in indices]
if shuffle:
random.seed(seed)
random.shuffle(examples)
if size:
examples = examples[:size]
if names:
if len(names) < size:
raise ValueError("altair-minigallery: if names are specified, "
"the list must be at least as long as size.")
mapping = {example['name']: example for example in examples}
examples = [mapping[name] for name in names]
else:
if indices:
examples = [examples[i] for i in indices]
if shuffle:
random.seed(seed)
random.shuffle(examples)
if size:
examples = examples[:size]

include = MINIGALLERY_TEMPLATE.render(image_dir='/_static',
gallery_dir=gallery_dir,
Expand Down
6 changes: 3 additions & 3 deletions altair/vegalite/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def test_basic_chart_from_dict(alt, basic_spec):

@pytest.mark.parametrize('alt', [v1, v2])
def test_theme_enable(alt):
active_theme = alt.theme.active
active_theme = alt.themes.active

try:
alt.theme.enable('none')
alt.themes.enable('none')

chart = alt.Chart.from_dict(basic_spec)
dct = chart.to_dict()
Expand All @@ -78,4 +78,4 @@ def test_theme_enable(alt):
assert dct == basic_spec
finally:
# reset the theme to its initial value
alt.theme.enable(active_theme)
alt.themes.enable(active_theme)
4 changes: 2 additions & 2 deletions altair/vegalite/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .data import data_transformers, pipe
from ... import utils
from .display import renderers, VEGALITE_VERSION, VEGA_VERSION, VEGAEMBED_VERSION
from .theme import theme
from .theme import themes


def _get_channels_mapping():
Expand Down Expand Up @@ -89,7 +89,7 @@ def to_dict(self, *args, **kwargs):
dct['$schema'] = SCHEMA_URL

# apply theme from theme registry
dct = utils.update_nested(theme.get(), dct, copy=True)
dct = utils.update_nested(themes.get(), dct, copy=True)
return dct

def savechart(self, fp, format=None, **kwargs):
Expand Down
8 changes: 4 additions & 4 deletions altair/vegalite/v1/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
class ThemeRegistry(PluginRegistry[dict]):
pass

theme = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP)
themes = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP)

theme.register('default', {"width": 400, "height": 300})
theme.register('none', {})
theme.enable('default')
themes.register('default', {"width": 400, "height": 300})
themes.register('none', {})
themes.enable('default')
4 changes: 2 additions & 2 deletions altair/vegalite/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .data import data_transformers, pipe
from ... import utils, expr
from .display import renderers, VEGALITE_VERSION, VEGAEMBED_VERSION, VEGA_VERSION
from .theme import theme
from .theme import themes

# ------------------------------------------------------------------------
# Data Utilities
Expand Down Expand Up @@ -343,7 +343,7 @@ def to_dict(self, *args, **kwargs):
dct['$schema'] = SCHEMA_URL

# apply theme from theme registry
dct = utils.update_nested(theme.get(), dct, copy=True)
dct = utils.update_nested(themes.get(), dct, copy=True)

return dct

Expand Down
10 changes: 5 additions & 5 deletions altair/vegalite/v2/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,19 @@ def test_LookupData():

def test_themes():
chart = alt.Chart('foo.txt').mark_point()
active = alt.theme.active
active = alt.themes.active

try:
alt.theme.enable('default')
alt.themes.enable('default')
assert chart.to_dict()['config'] == {"view": {"width": 400, "height": 300}}

alt.theme.enable('opaque')
alt.themes.enable('opaque')
assert chart.to_dict()['config'] == {"background": "white",
"view": {"width": 400, "height": 300}}

alt.theme.enable('none')
alt.themes.enable('none')
assert 'config' not in chart.to_dict()

finally:
# re-enable the original active theme
alt.theme.enable(active)
alt.themes.enable(active)
12 changes: 6 additions & 6 deletions altair/vegalite/v2/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class ThemeRegistry(PluginRegistry[dict]):
pass

theme = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP)
themes = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP)

theme.register('default', {"config": {"view": {"width": 400, "height": 300}}})
theme.register('opaque', {"config": {"background": "white",
"view": {"width": 400, "height": 300}}})
theme.register('none', {})
theme.enable('default')
themes.register('default', {"config": {"view": {"width": 400, "height": 300}}})
themes.register('opaque', {"config": {"background": "white",
"view": {"width": 400, "height": 300}}})
themes.register('none', {})
themes.enable('default')
3 changes: 1 addition & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ Altair: Declarative Visualization in Python
===========================================

.. altair-minigallery::
:names: one_dot_per_zipcode, horizon_graph, world_projections, candlestick_chart, falkensee, scatter_linked_brush, layered_heatmap_text, natural_disasters, streamgraph, multiline_tooltip, select_detail, choropleth, interactive_cross_highlight, seattle_weather_interactive, london_tube
:size: 15
:shuffle:
:seed: 31415

Altair is a declarative statistical visualization library for Python, based on
Vega_ and Vega-Lite_, and the source is available on
Expand Down

0 comments on commit 83291b5

Please sign in to comment.