diff --git a/packages/python/plotly/_plotly_utils/basevalidators.py b/packages/python/plotly/_plotly_utils/basevalidators.py index 0ade83a950..a2fb4e8ee4 100644 --- a/packages/python/plotly/_plotly_utils/basevalidators.py +++ b/packages/python/plotly/_plotly_utils/basevalidators.py @@ -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 ) diff --git a/packages/python/plotly/plotly/basedatatypes.py b/packages/python/plotly/plotly/basedatatypes.py index 257307b3b2..22469d4303 100644 --- a/packages/python/plotly/plotly/basedatatypes.py +++ b/packages/python/plotly/plotly/basedatatypes.py @@ -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): diff --git a/packages/python/plotly/plotly/io/_templates.py b/packages/python/plotly/plotly/io/_templates.py index 34110d09a0..8395a20dff 100644 --- a/packages/python/plotly/plotly/io/_templates.py +++ b/packages/python/plotly/plotly/io/_templates.py @@ -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 diff --git a/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py index 47dfffb77f..47bbf12b06 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py +++ b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py @@ -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" diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py index 08d4430c1f..b644040a5f 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py @@ -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(