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

Skip to content

Commit 253228f

Browse files
committed
grid_id is set to grids created via get_grid
1 parent 6f12ba8 commit 253228f

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,17 @@ class Grid(MutableSequence):
118118
py.plot([trace], filename='graph from grid')
119119
```
120120
"""
121-
def __init__(self, columns_or_json):
121+
def __init__(self, columns_or_json, grid_id=None):
122122
"""
123123
Initialize a grid with an iterable of `plotly.grid_objs.Column`
124124
objects or a json/dict describing a grid. See second usage example
125125
below for the necessary structure of the dict.
126126
127+
:param (str|bool) grid_id: should not be accessible to users. Default
128+
is 'None' but if a grid is retrieved via `py.get_grid()` then the
129+
retrieved grid response will contain the grid_id which will be
130+
necessary to set `self.id` and `self._columns.id` below.
131+
127132
Example from iterable of columns:
128133
```
129134
column_1 = Column([1, 2, 3], 'time')
@@ -141,9 +146,19 @@ def __init__(self, columns_or_json):
141146
grid = Grid(grid_json)
142147
```
143148
"""
144-
145149
# TODO: verify that columns are actually columns
146150
if isinstance(columns_or_json, dict):
151+
# check that grid_id is entered
152+
if grid_id is None:
153+
raise exceptions.PlotlyError(
154+
"If you are manually converting a raw json/dict grid "
155+
"into a Grid instance, you must ensure that make "
156+
"'grid_id' is set to your file ID. This looks like "
157+
"'username:187'."
158+
)
159+
# TODO: verify that grid_id is a correct fid if a string
160+
self.id = grid_id
161+
147162
# check if 'cols' is a root key
148163
if 'cols' not in columns_or_json:
149164
raise exceptions.PlotlyError(
@@ -182,7 +197,8 @@ def __init__(self, columns_or_json):
182197

183198
# fill in uids
184199
for column in self:
185-
column.id = columns_or_json['cols'][column.name]['uid']
200+
column.id = self.id + ':' + columns_or_json['cols'][column.name]['uid']
201+
186202
else:
187203
column_names = [column.name for column in columns_or_json]
188204
duplicate_name = utils.get_first_duplicate(column_names)
@@ -193,8 +209,6 @@ def __init__(self, columns_or_json):
193209
self._columns = list(columns_or_json)
194210
self.id = ''
195211

196-
#self._fid = ''
197-
198212
def __repr__(self):
199213
return self._columns.__repr__()
200214

@@ -241,7 +255,7 @@ def get_column(self, column_name):
241255
if column.name == column_name:
242256
return column
243257

244-
def get_uid(self, column_name):
258+
def get_fid_uid(self, column_name):
245259
"""
246260
Return uid of given column name in the grid by column name.
247261

plotly/plotly/plotly.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,6 @@ def upload(cls, grid, filename,
977977
cls._fill_in_response_column_ids(grid, response_columns, grid_id)
978978

979979
grid.id = grid_id
980-
#grid._fid = res['file']['fid']
981980

982981
if meta is not None:
983982
meta_ops.upload(meta, grid=grid)
@@ -1476,13 +1475,20 @@ def get_grid(grid_url, raw=False):
14761475
if url_path[-1] == '/':
14771476
url_path = url_path[0: -1]
14781477
url_path = url_path.replace('/', ':')
1479-
get_url = upload_url + '/' + url_path + '/content'
1480-
print url_path
1478+
1479+
meta_get_url = upload_url + '/' + url_path
1480+
get_url = meta_get_url + '/content'
14811481

14821482
r = requests.get(get_url, headers=headers)
14831483
json_res = json.loads(r.text)
1484+
1485+
# make request to grab the grid id (fid)
1486+
r_meta = requests.get(meta_get_url, headers=headers)
1487+
json_res_meta = json.loads(r_meta.text)
1488+
retrieved_grid_id = json_res_meta['fid']
1489+
14841490
if raw is False:
1485-
return Grid(json_res)
1491+
return Grid(json_res, retrieved_grid_id)
14861492
else:
14871493
return json_res
14881494

0 commit comments

Comments
 (0)