Skip to content

Template specification fixes #1819

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

Merged
merged 2 commits into from
Oct 15, 2019
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
5 changes: 4 additions & 1 deletion packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ def update(self, dict1=None, **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