forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_json.py
200 lines (158 loc) · 5.47 KB
/
_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from __future__ import absolute_import
from six import string_types
import json
from plotly.utils import PlotlyJSONEncoder
from plotly.io._utils import (validate_coerce_fig_to_dict,
validate_coerce_output_type)
def to_json(fig,
validate=True,
pretty=False,
remove_uids=True):
"""
Convert a figure to a JSON string representation
Parameters
----------
fig:
Figure object or dict representing a figure
validate: bool (default True)
True if the figure 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
Returns
-------
str
Representation of figure as a JSON string
"""
# Validate figure
# ---------------
fig_dict = validate_coerce_fig_to_dict(fig, validate)
# Remove trace uid
# ----------------
if remove_uids:
for trace in fig_dict.get('data', []):
trace.pop('uid', None)
# Dump to a JSON string and return
# --------------------------------
opts = {'sort_keys': True}
if pretty:
opts['indent'] = 2
else:
# Remove all whitespace
opts['separators'] = (',', ':')
return json.dumps(fig_dict, cls=PlotlyJSONEncoder, **opts)
def write_json(fig, file, validate=True, pretty=False, remove_uids=True):
"""
Convert a figure to JSON and write it to a file or writeable
object
Parameters
----------
fig:
Figure object or dict representing a figure
file: str or writeable
A string representing a local file path or a writeable object
(e.g. an open file descriptor)
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
Returns
-------
None
"""
# Get JSON string
# ---------------
# Pass through validate argument and let to_json handle validation logic
json_str = to_json(
fig, validate=validate, pretty=pretty, remove_uids=remove_uids)
# Check if file is a string
# -------------------------
file_is_str = isinstance(file, string_types)
# Open file
# ---------
if file_is_str:
with open(file, 'w') as f:
f.write(json_str)
else:
file.write(json_str)
def from_json(value, output_type='Figure', skip_invalid=False):
"""
Construct a figure from a JSON string
Parameters
----------
value: str
String containing the JSON representation of a figure
output_type: type or str (default 'Figure')
The output figure type or type name.
One of: graph_objs.Figure, 'Figure',
graph_objs.FigureWidget, 'FigureWidget'
skip_invalid: bool (default False)
False if invalid figure properties should result in an exception.
True if invalid figure properties should be silently ignored.
Raises
------
ValueError
if value is not a string, or if skip_invalid=False and value contains
invalid figure properties
Returns
-------
Figure or FigureWidget
"""
# Validate value
# --------------
if not isinstance(value, string_types):
raise ValueError("""
from_json requires a string argument but received value of type {typ}
Received value: {value}""".format(typ=type(value),
value=value))
# Decode JSON
# -----------
fig_dict = json.loads(value)
# Validate coerce output type
# ---------------------------
cls = validate_coerce_output_type(output_type)
# Create and return figure
# ------------------------
fig = cls(fig_dict, skip_invalid=skip_invalid)
return fig
def read_json(file, output_type='Figure', skip_invalid=False):
"""
Construct a figure from the JSON contents of a local file or readable
Python object
Parameters
----------
file: str or readable
A string containing the path to a local file or a read-able Python
object (e.g. an open file descriptor)
output_type: type or str (default 'Figure')
The output figure type or type name.
One of: graph_objs.Figure, 'Figure',
graph_objs.FigureWidget, 'FigureWidget'
skip_invalid: bool (default False)
False if invalid figure properties should result in an exception.
True if invalid figure properties should be silently ignored.
Returns
-------
Figure or FigureWidget
"""
# Check if file is a string
# -------------------------
# If it's a string we assume it's a local file path. If it's not a string
# then we assume it's a read-able Python object
file_is_str = isinstance(file, string_types)
# Read file contents into JSON string
# -----------------------------------
if file_is_str:
with open(file, 'r') as f:
json_str = f.read()
else:
json_str = file.read()
# Construct and return figure
# ---------------------------
return from_json(json_str,
skip_invalid=skip_invalid,
output_type=output_type)