Skip to content

Commit dbb386f

Browse files
committed
fixed order upon creating Grid
1 parent 4fbdc99 commit dbb386f

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,11 @@ def __init__(self, columns_or_json):
143143
"""
144144

145145
# TODO: verify that columns are actually columns
146-
if isinstance(columns_or_json, (list, tuple)):
147-
column_names = [column.name for column in columns_or_json]
148-
duplicate_name = utils.get_first_duplicate(column_names)
149-
if duplicate_name:
150-
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
151-
raise exceptions.InputError(err)
152-
153-
self._columns = list(columns_or_json)
154-
self.id = ''
155-
elif isinstance(columns_or_json, dict):
156-
# check if 'cols' is only root key
146+
if isinstance(columns_or_json, dict):
147+
# check if 'cols' is a root key
157148
if 'cols' not in columns_or_json:
158149
raise exceptions.PlotlyError(
159-
"'cols' must be the one and only key in your json grid."
150+
"'cols' must be a root key in your json grid."
160151
)
161152

162153
# check if 'data', 'order' and 'uid' are not in columns
@@ -169,12 +160,31 @@ def __init__(self, columns_or_json):
169160
"Each column name of your dictionary must have "
170161
"'data', 'order' and 'uid' as keys."
171162
)
163+
# order columns in a list before putting inside the grid
164+
ordered_columns = []
165+
for order in range(len(columns_or_json['cols'])):
166+
for column_name in columns_or_json['cols'].keys():
167+
if columns_or_json['cols'][column_name]['order'] == order:
168+
break
169+
170+
ordered_columns.append(Column(
171+
columns_or_json['cols'][column_name]['data'],
172+
column_name)
173+
)
174+
self._columns = ordered_columns
172175

173-
self._columns = [Column(columns_or_json['cols'][column_name]['data'], column_name)
174-
for column_name in columns_or_json['cols']]
175176
# fill in uids
176177
for column in self:
177178
column.id = columns_or_json['cols'][column.name]['uid']
179+
else:
180+
column_names = [column.name for column in columns_or_json]
181+
duplicate_name = utils.get_first_duplicate(column_names)
182+
if duplicate_name:
183+
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
184+
raise exceptions.InputError(err)
185+
186+
self._columns = list(columns_or_json)
187+
self.id = ''
178188

179189
def __repr__(self):
180190
return self._columns.__repr__()

0 commit comments

Comments
 (0)