Skip to content

Propagate empty templates #1892

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
Nov 11, 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
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 @@ -1945,7 +1945,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