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

Skip to content

Commit ec16b45

Browse files
committed
mv column encoder into plotly json encoder
1 parent 859b5d2 commit ec16b45

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

plotly/grid_objs/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66

77

88
from . grid_objs import Grid, Column
9-
from . import grid_objs_tools

plotly/grid_objs/grid_objs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
from collections import MutableSequence
99
from plotly import exceptions
10-
from plotly import utils
10+
import plotly
1111

1212

1313
class Column(object):
@@ -21,7 +21,7 @@ def __init__(self, data, name):
2121

2222
def __str__(self):
2323
max_chars = 10
24-
jdata = json.dumps(self.data)
24+
jdata = json.dumps(self.data, cls=plotly.utils._plotlyJSONEncoder)
2525
if len(jdata) > max_chars:
2626
data_string = jdata[:max_chars] + "...]"
2727
else:
@@ -30,14 +30,13 @@ def __str__(self):
3030
return string.format(name=self.name, data=data_string, id=self.id)
3131

3232
def __repr__(self):
33-
return ('Column("{}", {})'
34-
.format(data=self.data, name=self.name))
33+
return 'Column("{}", {})'.format(self.data, self.name)
3534

3635

3736
class Grid(MutableSequence):
3837
def __init__(self, iterable_of_columns):
3938
column_names = [column.name for column in iterable_of_columns]
40-
duplicate_name = utils.get_first_duplicate(column_names)
39+
duplicate_name = plotly.utils.get_first_duplicate(column_names)
4140
if duplicate_name:
4241
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
4342
raise exceptions.InputError(err)

plotly/plotly/plotly.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from urlparse import urlparse
2727

2828
from plotly.plotly import chunked_requests
29-
from plotly.grid_objs.grid_objs_tools import ColumnJSONEncoder
3029
from plotly import utils
3130
from plotly import tools
3231
from plotly import exceptions
@@ -748,7 +747,7 @@ def append_columns(cls, columns, grid=None, grid_url=None):
748747
raise exceptions.InputError(err)
749748

750749
payload = {
751-
'cols': json.dumps(columns, cls=ColumnJSONEncoder)
750+
'cols': json.dumps(columns, cls=utils._plotlyJSONEncoder)
752751
}
753752

754753
api_url = _api_v2.api_url('grids')+'/{grid_id}/col'.format(grid_id=grid_id)

plotly/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import threading
1313
import re
1414

15+
import grid_objs
16+
1517
### incase people are using threading, we lock file reads
1618
lock = threading.Lock()
1719

@@ -126,12 +128,20 @@ def sageJSONEncoder(self, obj):
126128
pass
127129
return None
128130

131+
def ColumnJSONEncoder(self, obj):
132+
if isinstance(obj, grid_objs.Column):
133+
return {'name': obj.name, 'data': obj.data}
134+
else:
135+
return None
136+
137+
129138
def default(self, obj):
130139
try:
131140
return json.dumps(obj)
132141
except TypeError as e:
133142
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder,
134-
self.pandasJSONEncoder, self.sageJSONEncoder)
143+
self.pandasJSONEncoder, self.ColumnJSONEncoder,
144+
self.sageJSONEncoder)
135145
for encoder in encoders:
136146
s = encoder(obj)
137147
if s is not None:

0 commit comments

Comments
 (0)