write_image() doesn't work with pathlib.Path() objects
See original GitHub issueThis code works as expected:
import plotly.express as px
fig = px.scatter(x=[1,2,3], y=[1,2,3])
# Works with a string
fig.write_image('test.png', format='png')
However, if you try to use a pathlib.Path object instead of a string, you get an exception:
import plotly.express as px
import pathlib
fig = px.scatter(x=[1,2,3], y=[1,2,3])
# Doesn't work with a Path
fig.write_image(pathlib.Path('test.png'), format='png')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-402d35877fa7> in <module>
6 fig.write_image('test.png', format='png')
7 # Doesn't work with a Path
----> 8 fig.write_image(pathlib.Path('test.png'), format='png')
/opt/conda/lib/python3.8/site-packages/plotly/basedatatypes.py in write_image(self, *args, **kwargs)
3249 import plotly.io as pio
3250
-> 3251 return pio.write_image(self, *args, **kwargs)
3252
3253 # Static helpers
/opt/conda/lib/python3.8/site-packages/plotly/io/_kaleido.py in write_image(fig, file, format, scale, width, height, validate, engine)
258 f.write(img_data)
259 else:
--> 260 file.write(img_data)
261
262
AttributeError: 'PosixPath' object has no attribute 'write'
This code works, though:
import plotly.express as px
import pathlib
fig = px.scatter(x=[1,2,3], y=[1,2,3])
fig.write_image(str(pathlib.Path('test.png')), format='png')
So, at a minimum this could be fixed by checking if file
is an instance of pathlib.Path and if so, cast it as a str.
PS Thanks for all the hard work on this project, it’s great watching it continually improve. This isn’t a particularly important issue for me, just wanted to add it to the list of potential fixes for the next version in case others are also interested.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:9 (6 by maintainers)
Top Results From Across the Web
plotly.io.write_image — 5.11.0 documentation
file (str or writeable) – A string representing a local file path or a writeable object (e.g. a pathlib.Path object or an open...
Read more >pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Read more >Imageio's user API — imageio 2.8.0 documentation
The resource to load the images from, e.g. a filename,pathlib.Path, http address or file object, see the docs for more info. format :...
Read more >Iterate Over Files (PDFs) to Run a Function - Stack Overflow
There are two issues. with open(os.path.join(os.getcwd(), filename),'rb') as f: # open/read the PDF files is not needed, f is never used ...
Read more >Image could not be saved Python - Blender Stack Exchange
There are builtin python libraries like pathlib for path manipulation. Don't forget to cast it back to a str before using it with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Support for this just came out with version 5.0 😃
Thanks for the bug report and the attempted PR 😃
I wonder if there’s not a more generic solution than spot-checking for
pathlib
objects, like “if it’s not writeable, try to stringify it” or something like that.