Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_plotly_json does not handle np.array properly #4281

Closed
ChiQiao opened this issue Jul 14, 2023 · 6 comments
Closed

to_plotly_json does not handle np.array properly #4281

ChiQiao opened this issue Jul 14, 2023 · 6 comments

Comments

@ChiQiao
Copy link

ChiQiao commented Jul 14, 2023

Code to reproduce the issue:

import json
import plotly.graph_objects as go
import numpy as np

fig = go.Figure(go.Scatter(x=np.array([0, 1]), y=np.array([0, 1])))
json.dumps(fig.to_plotly_json())  # TypeError: Object of type ndarray is not JSON serializable
@itsluketwist
Copy link
Contributor

@ChiQiao - Hey! Whilst the to_plotly_json method doesn't provide a dict that is immediately json compatible, if you use the PlotlyJSONEncoder as the cls arg when creating the json then everything should work fine. 😄

Fixed code snippet:

import json
import plotly.graph_objects as go
import numpy as np

from plotly.utils import PlotlyJSONEncoder  # need this import

fig = go.Figure(go.Scatter(x=np.array([0, 1]), y=np.array([0, 1])))
json.dumps(fig.to_plotly_json(), cls=PlotlyJSONEncoder)  # encoder used as arg here, handles the numpy data  (amongst others)

Hopefully this fixes your issue!

@ChiQiao
Copy link
Author

ChiQiao commented Jul 27, 2023

@itsluketwist Thanks for the information. Do you think this is the expected behavior (i.e., fulfilling the purpose of this method as Convert figure to a JSON representation as a Python dict)? If so, please feel free to close this issue.

@itsluketwist
Copy link
Contributor

@ChiQiao - agreed, this experience could be better. I've made a PR to hopefully make it so, and discuss the json usage with project maintainers. 😄

@ChiQiao
Copy link
Author

ChiQiao commented Jul 28, 2023

@ChiQiao - agreed, this experience could be better. I've made a PR to hopefully make it so, and discuss the json usage with project maintainers. 😄

Awesome. As a side note, do you know the difference between the to_json and to_plotly_json methods, besides the fact that one returns str and the other returns dict? Based on the simple test below, the underlying dictionary is the same.

fig = go.Figure(go.Scatter(x=np.array([0, 0]), y=np.array([0, 1]), marker={"color": [0, np.nan]}))
json.loads(json.dumps(fig.to_plotly_json(), cls=PlotlyJSONEncoder)) == json.loads(fig.to_json())  # True

@itsluketwist
Copy link
Contributor

@ChiQiao - I don't know the reason for the multiple very similar functions here, especially when there is also a to_dict method that is the same as to_plotly_json. 😅

You're right that the to_json method should also fix your problem! 😄

@alexcjohnson
Copy link
Collaborator

Closed by #4301 - thanks @itsluketwist 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants