Skip to content

Commit cce6e9f

Browse files
Ben Mares (m2-jupyter)maresb
Ben Mares (m2-jupyter)
authored andcommitted
Handle pathlib.Path in write_image
Closes plotly#2753
1 parent 03979d1 commit cce6e9f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

Diff for: packages/python/plotly/plotly/io/_kaleido.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def write_image(
156156
157157
file: str or writeable
158158
A string representing a local file path or a writeable object
159-
(e.g. an open file descriptor)
159+
(e.g. a pathlib.Path object or an open file descriptor)
160160
161161
format: str or None
162162
The desired image format. One of
@@ -258,7 +258,29 @@ def write_image(
258258
with open(file, "wb") as f:
259259
f.write(img_data)
260260
else:
261-
file.write(img_data)
261+
# Handle a pathlib.Path object
262+
# ----------------------------
263+
try:
264+
file.write_bytes(img_data)
265+
return
266+
except AttributeError:
267+
pass
268+
# Handle an open file descriptor
269+
# ------------------------------
270+
try:
271+
file.write(img_data)
272+
return
273+
except AttributeError:
274+
pass
275+
# Handlers failed
276+
# ---------------
277+
raise ValueError(
278+
"""
279+
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
280+
""".format(
281+
file=file
282+
)
283+
)
262284

263285

264286
def full_figure_for_development(fig, warn=True, as_dict=False):

0 commit comments

Comments
 (0)