Skip to content

Commit eb7dfb8

Browse files
committed
Refactor to use plotly.py to_json method for json encoding
This will provide future compatibility with the plotly.py 5.0 JSON engine, and accelerated orjson encoder.
1 parent 42234c3 commit eb7dfb8

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

repos/kaleido/py/kaleido/scopes/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515

1616
class BaseScope(object):
17-
# Subclasses may override to specify a custom JSON encoder for input data
18-
_json_encoder = None
19-
2017
# Tuple of class properties that will be passed as command-line
2118
# flags to configure scope
2219
_scope_flags = ()
@@ -274,6 +271,9 @@ def chromium_args(self, val):
274271
self._chromium_args = tuple(val)
275272
self._shutdown_kaleido()
276273

274+
def _json_dumps(self, val):
275+
return json.dumps(val)
276+
277277
def _perform_transform(self, data, **kwargs):
278278
"""
279279
Transform input data using the current scope, returning dict response with error code
@@ -287,9 +287,7 @@ def _perform_transform(self, data, **kwargs):
287287
self._ensure_kaleido()
288288

289289
# Perform export
290-
export_spec = json.dumps(
291-
dict(kwargs, data=data),
292-
cls=self._json_encoder).encode('utf-8')
290+
export_spec = self._json_dumps(dict(kwargs, data=data)).encode('utf-8')
293291

294292
# Write to process and read result within a lock so that can be
295293
# sure we're reading the response to our request

repos/kaleido/py/kaleido/scopes/plotly.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import absolute_import
22
from kaleido.scopes.base import BaseScope, which
3-
from _plotly_utils.utils import PlotlyJSONEncoder
3+
import plotly.io as pio
44
import base64
55
import os
66
from pathlib import Path
@@ -10,7 +10,6 @@ class PlotlyScope(BaseScope):
1010
"""
1111
Scope for transforming Plotly figures to static images
1212
"""
13-
_json_encoder = PlotlyJSONEncoder
1413
_all_formats = ("png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json")
1514
_text_formats = ("svg", "json", "eps")
1615

@@ -73,6 +72,9 @@ def _initialize_mathax(self, mathjax=None):
7372
def scope_name(self):
7473
return "plotly"
7574

75+
def _json_dumps(self, val):
76+
return pio.to_json(val, validate=False, remove_uids=False)
77+
7678
def transform(self, figure, format=None, width=None, height=None, scale=None):
7779
"""
7880
Convert a Plotly figure into a static image

0 commit comments

Comments
 (0)