Skip to content

Commit

Permalink
Template specification fixes (#1819)
Browse files Browse the repository at this point in the history
* Allow pio.template[] to lookup flaglist of templates, fixes #1807
* Fix fig.update to update layout with string
  • Loading branch information
jonmmease authored Oct 15, 2019
1 parent 240d0b4 commit e23076f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
5 changes: 4 additions & 1 deletion packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,10 @@ def update(self, dict1=None, overwrite=False, **kwargs):
else:
# Accept v
self[k] = v
elif isinstance(update_target, BasePlotlyType) or (
elif (
isinstance(update_target, BasePlotlyType)
and isinstance(v, (dict, BasePlotlyType))
) or (
isinstance(update_target, tuple)
and isinstance(update_target[0], BasePlotlyType)
):
Expand Down
48 changes: 30 additions & 18 deletions packages/python/plotly/plotly/io/_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import json
from functools import reduce

from six import string_types

try:
from math import gcd
except ImportError:
Expand Down Expand Up @@ -61,24 +63,34 @@ def __iter__(self):
return iter(self._templates)

def __getitem__(self, item):
template = self._templates[item]
if template is Lazy:
from plotly.graph_objs.layout import Template

if item == "none":
# "none" is a special built-in named template that applied no defaults
template = Template()
self._templates[item] = template
else:
# Load template from package data
path = os.path.join("package_data", "templates", item + ".json")
template_str = pkgutil.get_data("plotly", path).decode("utf-8")
template_dict = json.loads(template_str)
template = Template(template_dict)

self._templates[item] = template

return template
if isinstance(item, string_types):
template_names = item.split("+")
else:
template_names = [item]

templates = []
for template_name in template_names:
template = self._templates[template_name]
if template is Lazy:
from plotly.graph_objs.layout import Template

if template_name == "none":
# "none" is a special built-in named template that applied no defaults
template = Template()
self._templates[template_name] = template
else:
# Load template from package data
path = os.path.join(
"package_data", "templates", template_name + ".json"
)
template_str = pkgutil.get_data("plotly", path).decode("utf-8")
template_dict = json.loads(template_str)
template = Template(template_dict)

self._templates[template_name] = template
templates.append(self._templates[template_name])

return self.merge_templates(*templates)

def __setitem__(self, key, value):
self._templates[key] = self._validate(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ def test_merge_by_flaglist_string(self):
self.assertEqual(self.template1, self.template1_orig)
self.assertEqual(self.template2, self.template2_orig)

def test_flaglist_string_getitem(self):
result = pio.templates["template1+template2"]
expected = self.expected1_2
self.assertEqual(result, expected)

def test_update_template_with_flaglist(self):
fig = go.Figure()
fig.update(layout_template="template1+template2")
result = fig.layout.template
expected = self.expected1_2
self.assertEqual(result, expected)

def test_set_default_template(self):
orig_default = pio.templates.default
pio.templates.default = "plotly"
Expand Down

0 comments on commit e23076f

Please sign in to comment.