Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 859b6de

Browse files
committed
Fixes plotly#414 , need to add tests though
1 parent 512c401 commit 859b6de

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

plotly/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import sys
1414
import threading
15+
import decimal
1516

1617
import pytz
1718

@@ -130,6 +131,9 @@ def coerce_to_strict(self, const):
130131
# before python 2.7, 'true', 'false', 'null', were include here.
131132
if const in ('Infinity', '-Infinity', 'NaN'):
132133
return None
134+
# Decimal Fails on Serialization, convert it to float
135+
elif isinstance(const, decimal.Decimal):
136+
return float(const)
133137
else:
134138
return const
135139

@@ -201,7 +205,8 @@ def default(self, obj):
201205
self.encode_as_pandas,
202206
self.encode_as_datetime,
203207
self.encode_as_date,
204-
self.encode_as_list # because some values have `tolist` do last.
208+
self.encode_as_list, # because some values have `tolist` do last.
209+
self.encode_as_decimal
205210
)
206211
for encoding_method in encoding_methods:
207212
try:
@@ -296,6 +301,13 @@ def encode_as_date(obj):
296301
else:
297302
return iso_to_plotly_time_string(time_string)
298303

304+
@staticmethod
305+
def encode_as_decimal(obj):
306+
"""Attempt to encode decimal by converting it to float"""
307+
if isinstance(obj, decimal.Decimal):
308+
return float(obj)
309+
else:
310+
raise NotEncodable
299311

300312
### unicode stuff ###
301313
def decode_unicode(coll):

0 commit comments

Comments
 (0)