diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298..18bf4f8597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed +- Fix `plotly.io.to_templated` dropping title style properties such as `title.font` instead of moving them to the template; only the title text is kept out of the template now [[#5924](https://github.com/plotly/plotly.py/pull/5924)] + ## [7.2.0] - 2026-11-24 diff --git a/plotly/io/_templates.py b/plotly/io/_templates.py index 160ee7c407..66af137862 100644 --- a/plotly/io/_templates.py +++ b/plotly/io/_templates.py @@ -342,7 +342,7 @@ def walk_push_to_template(fig_obj, template_obj, skip): pass -def to_templated(fig, skip=("title", "text")): +def to_templated(fig, skip=("text",)): """ Return a copy of a figure where all styling properties have been moved into the figure's template. The template property of the resulting figure @@ -354,9 +354,9 @@ def to_templated(fig, skip=("title", "text")): Figure object or dict representing a figure skip A collection of names of properties to skip when moving properties to - the template. Defaults to ('title', 'text') so that the text - of figure titles, axis titles, and annotations does not become part of - the template + the template. Defaults to ('text',) so that the text of figure + titles, axis titles, and annotations does not become part of the + template, while their other properties (such as the title font) do Examples -------- diff --git a/tests/test_core/test_graph_objs/test_template.py b/tests/test_core/test_graph_objs/test_template.py index e1edd5e076..5660d0e8c8 100644 --- a/tests/test_core/test_graph_objs/test_template.py +++ b/tests/test_core/test_graph_objs/test_template.py @@ -229,6 +229,38 @@ def test_move_layout_nested_properties_no_skip(self): ) self.assertEqual(templated_fig, expected_fig) + def test_move_layout_title_style_properties(self): + # Title style properties are moved to the template, but the title + # text is not (https://github.com/plotly/plotly.py/issues/3580) + fig = go.Figure( + layout={ + "title": { + "text": "Hello", + "font": {"family": "Times New Roman", "size": 24}, + "x": 0.5, + }, + "xaxis": {"title": {"text": "X", "font": {"color": "red"}}}, + } + ) + templated_fig = pio.to_templated(fig) + + expected_fig = go.Figure( + layout={ + "template": { + "layout": { + "title": { + "font": {"family": "Times New Roman", "size": 24}, + "x": 0.5, + }, + "xaxis": {"title": {"font": {"color": "red"}}}, + }, + }, + "title": {"text": "Hello"}, + "xaxis": {"title": {"text": "X"}}, + } + ) + self.assertEqual(templated_fig, expected_fig) + def test_move_layout_nested_with_existing_template(self): fig = go.Figure( layout={