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

Update to_plotly_json docstring, and add new to_json_string helper method #4301

Merged
merged 8 commits into from
Aug 22, 2023
37 changes: 37 additions & 0 deletions packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3318,6 +3318,8 @@ def to_plotly_json(self):
"""
Convert figure to a JSON representation as a Python dict

Note: May include some JSON-invalid data types, use the `PlotlyJSONEncoder` util when encoding.
itsluketwist marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
dict
Expand Down Expand Up @@ -5597,12 +5599,47 @@ def to_plotly_json(self):
"""
Return plotly JSON representation of object as a Python dict

Note: May include some JSON-invalid data types, use the `PlotlyJSONEncoder` util when encoding.

Returns
-------
dict
"""
return deepcopy(self._props if self._props is not None else {})

def to_json(self, *args, **kwargs):
"""
Convert object to a JSON string representation

Parameters
----------
validate: bool (default True)
True if the object should be validated before being converted to
JSON, False otherwise.

pretty: bool (default False)
True if JSON representation should be pretty-printed, False if
representation should be as compact as possible.

remove_uids: bool (default True)
True if trace UIDs should be omitted from the JSON representation

engine: str (default None)
The JSON encoding engine to use. One of:
- "json" for an encoder based on the built-in Python json module
- "orjson" for a fast encoder the requires the orjson package
If not specified, the default encoder is set to the current value of
plotly.io.json.config.default_encoder.

Returns
-------
str
Representation of object as a JSON string
"""
import plotly.io as pio

return pio.to_json(self, *args, **kwargs)

@staticmethod
def _vals_equal(v1, v2):
"""
Expand Down