10000 Better error message when orjson not installed and orjson engine requ… · afonit/plotly.py@67d3670 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67d3670

Browse files
committed
Better error message when orjson not installed and orjson engine requested
1 parent 101ba85 commit 67d3670

File tree

1 file changed

+11
-7
lines changed
  • packages/python/plotly/plotly/io

1 file changed

+11
-7
lines changed

packages/python/plotly/plotly/io/_json.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ def default_engine(self, val):
3232
)
3333

3434
if val == "orjson":
35-
orjson = get_module("orjson")
36-
if orjson is None:
37-
raise ValueError("The orjson engine requires the orjson package")
35+
self.validate_orjson()
3836

3937
self._default_engine = val
4038

39+
@classmethod
40+
def validate_orjson(cls):
41+
orjson = get_module("orjson")
42+
if orjson is None:
43+
raise ValueError("The orjson engine requires the orjson package")
44+
4145

4246
config = JsonConfig()
4347

@@ -106,8 +110,6 @@ def to_plotly_json(plotly_object, pretty=False, engine=None):
106110
"image": get_module("PIL.Image", should_load=False),
107111
}
108112

109-
orjson = get_module("orjson", should_load=True)
110-
111113
# Dump to a JSON string and return
112114
# --------------------------------
113115
if engine in ("json", "legacy"):
@@ -148,6 +150,7 @@ def to_plotly_json(plotly_object, pretty=False, engine=None):
148150

149151
return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
150152
elif engine == "orjson":
153+
JsonConfig.validate_orjson()
151154
opts = orjson.OPT_SORT_KEYS | orjson.OPT_SERIALIZE_NUMPY
152155

153156
if pretty:
@@ -287,6 +290,8 @@ def from_plotly_json(value, engine=None):
287290
-------
288291
dict
289292
"""
293+
orjson = get_module("orjson", should_load=True)
294+
290295
# Validate value
291296
# --------------
292297
if not isinstance(value, (string_types, bytes)):
@@ -298,8 +303,6 @@ def from_plotly_json(value, engine=None):
298303
)
299304
)
300305

301-
orjson = get_module("orjson", should_load=True)
302-
303306
# Determine json engine
304307
if engine is None:
305308
engine = config.default_engine
@@ -313,6 +316,7 @@ def from_plotly_json(value, engine=None):
313316
raise ValueError("Invalid json engine: %s" % engine)
314317

315318
if engine == "orjson":
319+
JsonConfig.validate_orjson()
316320
# orjson handles bytes input natively
317321
value_dict = orjson.loads(value)
318322
else:

0 commit comments

Comments
 (0)
0