Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly.image.get Return Format

Hello,

I am pretty new to Python. I found the syntax for using plotly.image.get() to return the bytes of an image but can’t find any documentation on how to convert the bytes into something usable. Ultimately, I’d like to decode the bytes and set them as the source of an tag in HTML to display an image without having to save it to the file system. My current experimental code is as follows:

fig = {"data": [Scatter(x=[1,2,3,4], y=[4,3,2,1])],"layout": Layout(title="hello world")}
plot = py.image.get(fig,format="png",height=600,width=800)

From here, I get a byte-like object returned but I don’t know what to do with it from there. I can’t convert it to utf8 as there are some invalid characters and it looks like there is other info returned then just the image’s raw byte data.

Any help would be appreciated,

Marcus

Figured it out. The HTML source is different from the byte source of an image. Had to base64 encode the bytes to convert to the correct HTML format.

Credit to source