Skip to content
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

Refactor JSON encoding to use plotly.py JSON engine #1514

Merged
merged 15 commits into from
Aug 16, 2021
Merged
7 changes: 7 additions & 0 deletions dash/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
_strings = (type(""), type(utils.bytes_to_native_str(b"")))


def to_json(value):
# pylint: disable=import-outside-toplevel
from plotly.io.json import to_json_plotly

return to_json_plotly(value)


def interpolate_str(template, **data):
s = template
for k, v in data.items():
Expand Down
12 changes: 4 additions & 8 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys
import collections
import importlib
import json
import pkgutil
import threading
import re
Expand All @@ -22,8 +21,6 @@
from werkzeug.debug.tbtools import get_current_traceback
from pkg_resources import get_distribution, parse_version

import plotly

from .fingerprint import build_fingerprint, check_fingerprint
from .resources import Scripts, Css
from .dependencies import (
Expand All @@ -49,6 +46,7 @@
split_callback_id,
stringify_id,
strip_relative_path,
to_json,
)
from . import _dash_renderer
from . import _validate
Expand Down Expand Up @@ -547,7 +545,7 @@ def serve_layout(self):

# TODO - Set browser cache limit - pass hash into frontend
return flask.Response(
json.dumps(layout, cls=plotly.utils.PlotlyJSONEncoder),
to_json(layout),
mimetype="application/json",
)

Expand Down Expand Up @@ -705,7 +703,7 @@ def _generate_scripts_html(self):

def _generate_config_html(self):
return '<script id="_dash-config" type="application/json">{}</script>'.format(
json.dumps(self._config(), cls=plotly.utils.PlotlyJSONEncoder)
to_json(self._config())
)

def _generate_renderer(self):
Expand Down Expand Up @@ -1100,9 +1098,7 @@ def add_context(*args, **kwargs):
response = {"response": component_ids, "multi": True}

try:
jsonResponse = json.dumps(
response, cls=plotly.utils.PlotlyJSONEncoder
)
jsonResponse = to_json(response)
except TypeError:
_validate.fail_callback_output(output_value, output)

Expand Down
2 changes: 1 addition & 1 deletion requires-install.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Flask>=1.0.4
flask-compress
plotly
plotly>=5.0.0
dash-core-components==1.17.1
dash-html-components==1.1.4
dash-table==4.12.0
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/callbacks/test_malformed_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def update_output(value):
),
)
assert response.status_code == 200
assert '"o1": {"children": 9}' in response.text
assert '"o1":{"children":9}' in response.text

# now some bad ones
outspecs = [
Expand Down