1

What's proper way to save plotly graph to local file with javascript?

img_png.attr("src", url);
Plotly.toImage(gd,{format:'png',height:400,width:400});

Above code from official document save it to element, I would like to save it to local disk directly!

lucky1928
  • 8,708
  • 10
  • 43
  • 92
  • `fig.write_image('filename')` For details, see Saving Images in [Reference](https://plotly.com/python/static-image-export/#write-image-file). – r-beginners Nov 27 '22 at 02:58

1 Answers1

1

You'll get an image encode with base64 string from Plotly.toImage(), and you can convert base64 string to image in many ways. (For example like this page.)

Assume the HTML code is like this:

<div id="chart">
  <!-- chart here -->
</div>

Then the JavaScript code should be like this:

Plotly.toImage('chart', { format: 'png', width: 800, height: 600 }).then(
    function (dataUrl) {
    // use the dataUrl
})
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Coco Yen
  • 140
  • 5
  • 10