plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

plotly.io.to_json method does not convert large numbers correctly

Open jimholl-dev opened this issue 9 months ago • 1 comments

Hi, I have some some code here, as below (edited to remove extra logs). I'm converting a plotly figure into json, and then loading it with json loads. You would expect that to preserve the original data.

Image'

However, as you can see, the "y" data (which had previously been an array of large numbers) are wrongly converted into this "bdata" thing instead.

Image

I was able to solve this and get the data in the format I wanted by going from 6.0.1 -> 5.24.1, which was the last stable build I was using. I think this is a pretty fundamental issue, so let me know if there's anything else I can share.

jimholl-dev avatar Apr 03 '25 14:04 jimholl-dev

This may not solve your issue, however I had a similar issue in a client application, rendering the json object using the plotly.js library.

It turned out I was using an outdated plotly javascript library, which in versions prior to v2.28 did not know about bdata (base64 encoded). Once I upgraded to the latest version of plotly.js it worked fine.

miraculixx avatar May 20 '25 19:05 miraculixx

@marthacryan suggests adding a base64: bool argument to plotly.io.to_json (similar to pretty: bool) that would control whether data is base64'd or not.

gvwilson avatar Jun 17 '25 16:06 gvwilson

For some context on implementation: the BaseFigure class does the conversion to base64 in the to_dict function here: https://github.com/plotly/plotly.py/blob/1ec864bbed58ee2834a879d323ade4b53bfde364/plotly/basedatatypes.py#L3344. So this is the call stack:

  1. to_json calls validate_coerce_fig_to_dict here
  2. validate_coerce_fig_to_dict calls BaseFigure.to_dict here.

So, we'd have to add a flag to plotly.io.to_json and validate_coerce_fig_to_dict and BaseFigure.to_dict.

marthacryan avatar Jun 18 '25 16:06 marthacryan