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
18 changes: 17 additions & 1 deletion packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from copy import deepcopy, copy
import itertools
from functools import reduce
import json

from _plotly_utils.utils import (
_natural_sort_strings,
Expand All @@ -17,6 +18,7 @@
display_string_positions,
chomp_empty_strings,
find_closest_string,
PlotlyJSONEncoder,
)
from _plotly_utils.exceptions import PlotlyKeyError
from .optional_imports import get_module
Expand Down Expand Up @@ -3316,7 +3318,9 @@ def to_dict(self):

def to_plotly_json(self):
"""
Convert figure to a JSON representation as a Python dict
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
-------
Expand Down Expand Up @@ -5597,12 +5601,24 @@ 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):
"""
Convert to a JSON string representation.

Returns
-------
str
"""
return json.dumps(self.to_plotly_json(), cls=PlotlyJSONEncoder)
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

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