Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/py/kaleido/_kaleido_tab/_tab.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import base64
from decimal import Decimal
from typing import TYPE_CHECKING

import logistro
Expand All @@ -27,6 +28,8 @@

def _orjson_default(obj):
"""Fallback for types orjson can't handle natively (e.g. NumPy string arrays)."""
if isinstance(obj, Decimal):
return float(obj)
if hasattr(obj, "tolist"):
return obj.tolist()
raise TypeError(f"Type is not JSON serializable: {type(obj).__name__}")
Expand Down
16 changes: 16 additions & 0 deletions src/py/tests/test_kaleido_tab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from decimal import Decimal

import orjson
import plotly.graph_objects as go

from kaleido._kaleido_tab._tab import _orjson_default
from kaleido._utils import fig_tools


def test_orjson_default_handles_decimal():
fig = go.Figure(data=[go.Bar(y=[Decimal("10.5"), Decimal(20), Decimal("-3.25")])])
spec = fig_tools.coerce_for_js(fig, None, {"format": "json"})

encoded = orjson.dumps(spec, default=_orjson_default)

assert b'"y":[10.5,20.0,-3.25]' in encoded