From e510c6d0bf09a6664088130148449d8bb798d3d4 Mon Sep 17 00:00:00 2001 From: Hayden Flinner Date: Tue, 13 Sep 2022 10:12:03 -0400 Subject: [PATCH 1/4] Prevent swallowing of ValueError in core.py Traceback that this leads to: ``` File .../python3.9/site-packages/plotly/express/_core.py:2226, in make_figure(args, constructor, trace_patch, layout_patch) 2223 if args.get("marginal_y") is not None: 2224 ncols += 1 -> 2226 fig = init_figure( 2227 args, subplot_type, frame_list, nrows, ncols, col_labels, row_labels 2228 ) 2230 # Position traces in subplots 2231 for frame in frame_list: File .../python3.9/site-packages/plotly/express/_core.py:2373, in init_figure(args, subplot_type, frame_list, nrows, ncols, col_labels, row_labels) 2370 _spacing_error_translator(e, "Vertical", "facet_row_spacing") 2372 # Remove explicit font size of row/col titles so template can take over -> 2373 for annot in fig.layout.annotations: 2374 annot.update(font=None) 2376 return fig UnboundLocalError: local variable 'fig' referenced before assignment ``` --- packages/python/plotly/plotly/express/_core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index dd2d53be1ed..5e00a06bff2 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -2368,6 +2368,7 @@ def _spacing_error_translator(e, direction, facet_arg): except ValueError as e: _spacing_error_translator(e, "Horizontal", "facet_col_spacing") _spacing_error_translator(e, "Vertical", "facet_row_spacing") + raise # Remove explicit font size of row/col titles so template can take over for annot in fig.layout.annotations: From 7250704776c730b66e283e7dbe394d0254327b18 Mon Sep 17 00:00:00 2001 From: Emily KL <4672118+emilykl@users.noreply.github.com> Date: Tue, 20 May 2025 13:44:13 -0400 Subject: [PATCH 2/4] fix indentation --- plotly/basedatatypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/basedatatypes.py b/plotly/basedatatypes.py index 12b14c1ca85..443c34e826c 100644 --- a/plotly/basedatatypes.py +++ b/plotly/basedatatypes.py @@ -3908,7 +3908,7 @@ def write_image(self, *args, **kwargs): warnings.warn( ENGINE_PARAM_DEPRECATION_MSG, DeprecationWarning, stacklevel=2 ) - return pio.write_image(self, *args, **kwargs) + return pio.write_image(self, *args, **kwargs) # Static helpers # -------------- From 6c257931c8a88a534a75a68b0ad571115244fb28 Mon Sep 17 00:00:00 2001 From: Emily KL <4672118+emilykl@users.noreply.github.com> Date: Tue, 20 May 2025 14:15:55 -0400 Subject: [PATCH 3/4] add test to prevent regression --- .../test_kaleido/test_kaleido.py | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/tests/test_optional/test_kaleido/test_kaleido.py b/tests/test_optional/test_kaleido/test_kaleido.py index 9a9d414dc92..84b07772307 100644 --- a/tests/test_optional/test_kaleido/test_kaleido.py +++ b/tests/test_optional/test_kaleido/test_kaleido.py @@ -1,15 +1,18 @@ +import base64 +from contextlib import redirect_stdout from io import BytesIO, StringIO from pathlib import Path import tempfile -from contextlib import redirect_stdout -import base64 +from unittest.mock import patch from pdfrw import PdfReader from PIL import Image +import plotly.graph_objects as go import plotly.io as pio from plotly.io.kaleido import kaleido_available, kaleido_major import pytest + fig = {"data": [], "layout": {"title": {"text": "figure title"}}} @@ -160,3 +163,43 @@ def test_defaults(): finally: pio.defaults.default_format = "png" assert pio.defaults.default_format == "png" + + +def test_fig_write_image(): + """Test that fig.write_image() calls the correct underlying Kaleido function.""" + + test_fig = go.Figure(fig) + test_image_bytes = b"mock image data" + + if kaleido_major() > 0: + patch_funcname = "plotly.io._kaleido.kaleido.calc_fig_sync" + else: + patch_funcname = "plotly.io._kaleido.scope.transform" + + with patch(patch_funcname, return_value=test_image_bytes) as mock_calc_fig: + test_fig.write_image("test_path.png") + + # Verify patched function was called once with fig dict as first argument + mock_calc_fig.assert_called_once() + args, _ = mock_calc_fig.call_args + assert args[0] == test_fig.to_dict() + + +def test_fig_to_image(): + """Test that fig.to_image() calls the correct underlying Kaleido function.""" + + test_fig = go.Figure(fig) + test_image_bytes = b"mock image data" + + if kaleido_major() > 0: + patch_funcname = "plotly.io._kaleido.kaleido.calc_fig_sync" + else: + patch_funcname = "plotly.io._kaleido.scope.transform" + + with patch(patch_funcname, return_value=test_image_bytes) as mock_calc_fig: + test_fig.to_image() + + # Verify patched function was called once with fig dict as first argument + mock_calc_fig.assert_called_once() + args, _ = mock_calc_fig.call_args + assert args[0] == test_fig.to_dict() From 17466877578987af9201a10afe3a354dd4217652 Mon Sep 17 00:00:00 2001 From: Emily KL <4672118+emilykl@users.noreply.github.com> Date: Tue, 20 May 2025 14:52:15 -0400 Subject: [PATCH 4/4] update pyproject.toml and changelog for 6.1.1 --- CHANGELOG.md | 6 ++++++ pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 688407f4649..c878960eeea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [6.1.1] - 2025-05-20 + +### Fixed +- Prevent swallowing of `ValueError` when creating a figure with subplots [[#3888](https://github.com/plotly/plotly.py/pull/3888)] +- Fix issue causing `fig.write_image()` to not generate an image [[#5193](https://github.com/plotly/plotly.py/pull/5193)] + ## [6.1.0] - 2025-05-15 ### Updated diff --git a/pyproject.toml b/pyproject.toml index 6ffbbbee865..8d8e813caab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,7 @@ classifiers = [ ] requires-python = ">=3.8" license = {file="LICENSE.txt"} -version = "6.1.0" +version = "6.1.1" dependencies = [ "narwhals>=1.15.1", "packaging"