Skip to content

Fix issue breaking fig.write_image() #5193

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
May 20, 2025
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
2 changes: 1 addition & 1 deletion plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# --------------
Expand Down
47 changes: 45 additions & 2 deletions tests/test_optional/test_kaleido/test_kaleido.py
Original file line number Diff line number Diff line change
@@ -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"}}}


Expand Down Expand Up @@ -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()