8000 Update to_plotly_json docstring, and add new to_json_string helper method by itsluketwist · Pull Request #4301 · plotly/plotly.py · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 8 commits into from
Aug 22, 2023
Next Next commit
Update to_plotly_json docstring, and implement helper method to creat…
…e a json string easily.
  • Loading branch information
itsluketwist committed Jul 28, 2023
commit 092e8ba97cbb8c3edeb2df7d5e83c60ed536c54d
26 changes: 25 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,14 +3318,26 @@ 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.

Returns
-------
dict
"""
return self.to_dict()

def to_json_str(self):
"""
Convert to a JSON string representation.

Returns
-------
str
"""
return json.dumps(self.to_plotly_json(), cls=PlotlyJSONEncoder)

@staticmethod
def _to_ordered_dict(d, skip_uid=False):
"""
Expand Down Expand Up @@ -5603,6 +5617,16 @@ def to_plotly_json(self):
"""
return deepcopy(self._props if self._props is not None else {})

def to_json_str(self):
"""
Convert to a JSON string representation.

Returns
-------
str
"""
return json.dumps(self.to_plotly_json(), cls=PlotlyJSONEncoder)

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