17

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.

stefan
  • 90,330
  • 6
  • 25
  • 51
Shrom
  • 173
  • 1
  • 1
  • 5
  • From the documentation [here](https://plotly.com/python/static-image-export/) your line looks fine. Please check if the folder `images` exists and share your error message. Maybe `format='png'` is redundant, but should (hopfully) not cause any errors. – mosc9575 Feb 05 '22 at 12:48
  • Theres no error message. The terminal crashes. Also the images folder exists – Shrom Feb 05 '22 at 12:57
  • Check if you can update any of you packages. – mosc9575 Feb 05 '22 at 13:03
  • I installed everything just yesterday. – Shrom Feb 05 '22 at 13:08
  • Can you output the figure using another method (like `fig.show()`)? Does the figure object exist and is this well defined? – mosc9575 Feb 05 '22 at 13:17
  • yes the figure is visible with fig.show() and everything else works. Its just the write_image and to_image methods which cause the issue – Shrom Feb 05 '22 at 13:30
  • Did you try `fig.write_image("images/fig1.png")`? This uses `engine='auto'`? Did you try to uninstall and reinstall kaleido? `pip install -U kaleido` – mosc9575 Feb 05 '22 at 13:37
  • 3
    i did. and i also installed kaleido. i also tried this ```import plotly.io as pio pio.kaleido.scope.mathjax = None``` but still doesnt work – Shrom Feb 05 '22 at 13:41
  • This (`import plotly.io as pio` and `pio.kaleido.scope.mathjax = None`) was my next and final tip. Sorry. I do not know how to solve your problem. – mosc9575 Feb 05 '22 at 13:44
  • have the same problem still 6 months later. I am using poetry as well, not sure whether this is related. – Herbz Aug 18 '22 at 15:37

3 Answers3

16

Try this version of kaleido.

pip install kaleido==0.1.0post1

It works for me

Gillian Grayson
  • 316
  • 1
  • 9
  • Thanks! This worked for jupyter lab on Windows 10 Pro. Also if you have multiple python versions installed for jupyter lab, make sure you install kaleido into the libraries for the Python version you are using. – uniqueID Feb 10 '23 at 23:03
  • 1
    it works for me too, i am on Win 11, jupyter notebook in VScode. – yts61 Mar 08 '23 at 17:06
1

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
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • My versions of plotly and kaleido are the same as you mentioned. I tried using your method but even that froze the terminal and I cant even ctrl C out of it. Each command is working fine, the directory is being made, fig.show() works, but as soon as fig.write_image appears, the terminal crashes – Shrom Feb 05 '22 at 17:54
  • interesting - kaleidoscope wraps chromium in a headless manner. in your runtime env this must be freezing. runs for me in jupyter and vscode, same venv – Rob Raymond Feb 05 '22 at 19:29
  • So what do you reckon I should do now? – Shrom Feb 06 '22 at 05:24
  • It's got to be something in your environment... not sure what o/s your are using. I suggest run a 100% virtualised env (docker container or virtual box vm) and test out in that – Rob Raymond Feb 06 '22 at 11:37
  • yea i used it in replit and it works fine there. however the vscode issue still persists. Not able to understand whats the problem but i think i have to use replit only for this thing. Appreciate your help though – Shrom Feb 07 '22 at 08:34
0

Please follow what Gillian Grayson suggested. I am using Jupyter Notebook, Plotly version 5.13.0 and Kaleido 0.1.0.post1 and Python 3.8

Also add this to make saving figure faster

import plotly plotly.io.kaleido.scope.mathjax= None

  • Please add this type of answers as a comment – xlmaster Feb 20 '23 at 06:47
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33856420) – puchal Feb 22 '23 at 13:36