'Not able to save plotly plots using to_image or write_image

fig.write_image("images/fig1.png",format='png',engine='kaleido')

This makes my VSCode go bananas, the terminal hangs and the program stops then and there. Everything works fine if I remove just that line.

I want to save the plots as pngs, but it is not working. I have kaleido installed.



Solution 1:[1]

Try this version of kaleido.

pip install kaleido==0.1.0post1

It works for me

Solution 2:[2]

Complete MWE. png gets created as expected.

import plotly.graph_objects as go
import numpy as np
from pathlib import Path

f = Path.cwd().joinpath("images")
if not f.is_dir(): f.mkdir()
f = f.joinpath("fig1.png")

fig = go.Figure(go.Scatter(x=np.linspace(1,10,100), y=np.sin(np.linspace(-np.pi,np.pi, 100))))

fig.write_image(f,format='png',engine='kaleido')

versions

import plotly
import kaleido

print(plotly.__version__, kaleido.__version__)
5.5.0 0.2.1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gillian Grayson
Solution 2 Rob Raymond