Skip to content
5 changes: 1 addition & 4 deletions dashipy/dashipy/components/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class Plot(Component):
def to_dict(self) -> dict[str, Any]:
d = super().to_dict()
if self.figure is not None:
# TODO: this is stupid, but if using self.figure.to_dict()
# for plotly.express figures we get
# TypeError: Object of type ndarray is not JSON serializable
d.update(figure=json.loads(self.figure.to_json()))
d.update(figure=self.figure.to_dict())
else:
d.update(figure=None)
return d
6 changes: 4 additions & 2 deletions dashipy/dashipy/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import importlib
import json
import traceback
from typing import Any

Expand All @@ -12,6 +13,7 @@
from dashipy.context import Context
from dashipy.contribs import Panel
from dashipy.lib import Extension, Contribution
from dashipy.utils import NumpyJSONEncoder

DASHI_CONTEXT_KEY = "dashi.context"
DASHI_EXTENSIONS_KEY = "dashi.extensions"
Expand Down Expand Up @@ -117,7 +119,7 @@ def render_layout(
return

self.set_header("Content-Type", "text/json")
self.write({"result": component.to_dict()})
self.write(json.dumps({"result": component.to_dict()}, cls=NumpyJSONEncoder))


class CallbackHandler(DashiHandler):
Expand Down Expand Up @@ -172,7 +174,7 @@ def post(self):
)

self.set_header("Content-Type", "text/json")
self.write({"result": change_requests})
self.write(json.dumps({"result": change_requests}, cls=NumpyJSONEncoder))


def print_usage(app, port):
Expand Down
1 change: 1 addition & 0 deletions dashipy/dashipy/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .json_encoder import NumpyJSONEncoder
9 changes: 9 additions & 0 deletions dashipy/dashipy/utils/json_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json
import numpy as np


class NumpyJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return super().default(obj)