question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

write_image() doesn't work with pathlib.Path() objects

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
nicolaskruchtencommented, Jun 21, 2021

Support for this just came out with version 5.0 😃

1reaction
nicolaskruchtencommented, Sep 8, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found