Skip to content

Commit 092e8ba

Browse files
committed
Update to_plotly_json docstring, and implement helper method to create a json string easily.
1 parent e670c4b commit 092e8ba

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from copy import deepcopy, copy
99
import itertools
1010
from functools import reduce
11+
import json
1112

1213
from _plotly_utils.utils import (
1314
_natural_sort_strings,
@@ -17,6 +18,7 @@
1718
display_string_positions,
1819
chomp_empty_strings,
1920
find_closest_string,
21+
PlotlyJSONEncoder,
2022
)
2123
from _plotly_utils.exceptions import PlotlyKeyError
2224
from .optional_imports import get_module
@@ -3316,14 +3318,26 @@ def to_dict(self):
33163318

33173319
def to_plotly_json(self):
33183320
"""
3319-
Convert figure to a JSON representation as a Python dict
3321+
Convert figure to a JSON representation as a Python dict.
3322+
3323+
Note: May include some JSON-invalid data types, use the `PlotlyJSONEncoder` util when encoding.
33203324
33213325
Returns
33223326
-------
33233327
dict
33243328
"""
33253329
return self.to_dict()
33263330

3331+
def to_json_str(self):
3332+
"""
3333+
Convert to a JSON string representation.
3334+
3335+
Returns
3336+
-------
3337+
str
3338+
"""
3339+
return json.dumps(self.to_plotly_json(), cls=PlotlyJSONEncoder)
3340+
33273341
@staticmethod
33283342
def _to_ordered_dict(d, skip_uid=False):
33293343
"""
@@ -5603,6 +5617,16 @@ def to_plotly_json(self):
56035617
"""
56045618
return deepcopy(self._props if self._props is not None else {})
56055619

5620+
def to_json_str(self):
5621+
"""
5622+
Convert to a JSON string representation.
5623+
5624+
Returns
5625+
-------
5626+
str
5627+
"""
5628+
return json.dumps(self.to_plotly_json(), cls=PlotlyJSONEncoder)
5629+
56065630
@staticmethod
56075631
def _vals_equal(v1, v2):
56085632
"""

0 commit comments

Comments
 (0)