Skip to content

Commit

Permalink
Propagate empty templates (#1892)
Browse files Browse the repository at this point in the history
* Propagate empty templates
* Treat "none" template as {"data": {"scatter": [{}]}} as well
  • Loading branch information
jonmmease authored Nov 11, 2019
1 parent 085372a commit 0ccd082
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,13 @@ def validate_coerce(self, v, skip_invalid=False):
# v is un-hashable
pass

# Check for empty template
if v == {} or isinstance(v, self.data_class) and v.to_plotly_json() == {}:
# Replace empty template with {'data': {'scatter': [{}]}} so that we can
# tell the difference between an un-initialized template and a template
# explicitly set to empty.
return self.data_class(data_scatter=[{}])

return super(BaseTemplateValidator, self).validate_coerce(
v, skip_invalid=skip_invalid
)
2 changes: 1 addition & 1 deletion packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ def _initialize_layout_template(self):
if pio.templates.default is not None:
self._layout_obj.template = pio.templates.default
else:
self._layout_obj.template = {}
self._layout_obj.template = None

@property
def layout(self):
Expand Down
2 changes: 1 addition & 1 deletion packages/python/plotly/plotly/io/_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __getitem__(self, item):

if template_name == "none":
# "none" is a special built-in named template that applied no defaults
template = Template()
template = Template(data_scatter=[{}])
self._templates[template_name] = template
else:
# Load template from package data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_template_default_override(self):
def test_template_default_override_empty(self):
pio.templates.default = "test_template"
fig = go.Figure(layout={"template": {}})
self.assertEqual(fig.layout.template, go.layout.Template())
self.assertEqual(fig.layout.template, go.layout.Template(data_scatter=[{}]))

def test_delete_default_template(self):
pio.templates.default = "test_template"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_px_templates():

# accept objects in args
fig = px.scatter(template={})
assert fig.layout.template == go.layout.Template()
assert fig.layout.template == go.layout.Template(data_scatter=[{}])

# read colorway from the template
fig = px.scatter(
Expand Down

0 comments on commit 0ccd082

Please sign in to comment.