Skip to content

Commit 4fbdc99

Browse files
committed
cleaned up url parsing + moved json_to_grid functionality to __init__ in Grid()
1 parent 49bdc97 commit 4fbdc99

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,63 @@ class Grid(MutableSequence):
118118
py.plot([trace], filename='graph from grid')
119119
```
120120
"""
121-
def __init__(self, iterable_of_columns):
121+
def __init__(self, columns_or_json):
122122
"""
123-
Initialize a grid with an iterable of
124-
`plotly.grid_objs.Column objects
123+
Initialize a grid with an iterable of `plotly.grid_objs.Column`
124+
objects or a json/dict describing a grid. See second usage example
125+
below for the necessary structure of the dict.
125126
126-
Usage example:
127+
Example from iterable of columns:
127128
```
128129
column_1 = Column([1, 2, 3], 'time')
129130
column_2 = Column([4, 2, 5], 'voltage')
130131
grid = Grid([column_1, column_2])
131132
```
133+
Example from json grid
134+
```
135+
grid_json = {
136+
'cols': {
137+
'time': {'data': [1, 2, 3], 'order': 0, 'uid': '4cd7fc'},
138+
'voltage': {'data': [4, 2, 5], 'order': 1, 'uid': u'2744be'}
139+
}
140+
}
141+
grid = Grid(grid_json)
142+
```
132143
"""
133144

134145
# TODO: verify that columns are actually columns
135-
136-
column_names = [column.name for column in iterable_of_columns]
137-
duplicate_name = utils.get_first_duplicate(column_names)
138-
if duplicate_name:
139-
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
140-
raise exceptions.InputError(err)
141-
142-
self._columns = list(iterable_of_columns)
143-
self.id = ''
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
157+
if 'cols' not in columns_or_json:
158+
raise exceptions.PlotlyError(
159+
"'cols' must be the one and only key in your json grid."
160+
)
161+
162+
# check if 'data', 'order' and 'uid' are not in columns
163+
grid_col_keys = ['data', 'order', 'uid']
164+
165+
for column_name in columns_or_json['cols']:
166+
for key in grid_col_keys:
167+
if key not in columns_or_json['cols'][column_name]:
168+
raise exceptions.PlotlyError(
169+
"Each column name of your dictionary must have "
170+
"'data', 'order' and 'uid' as keys."
171+
)
172+
173+
self._columns = [Column(columns_or_json['cols'][column_name]['data'], column_name)
174+
for column_name in columns_or_json['cols']]
175+
# fill in uids
176+
for column in self:
177+
column.id = columns_or_json['cols'][column.name]['uid']
144178

145179
def __repr__(self):
146180
return self._columns.__repr__()

plotly/plotly/plotly.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from plotly.session import (sign_in, update_session_plot_options,
3434
get_session_plot_options, get_session_credentials,
3535
get_session_config)
36-
from plotly.grid_objs import grid_objs
36+
from plotly.grid_objs import Grid, Column
3737

3838
__all__ = None
3939

@@ -1470,26 +1470,17 @@ def get_grid(grid_url, raw=False):
14701470
'plotly-platform': 'python'}
14711471
upload_url = _api_v2.api_url('grids')
14721472

1473-
tilda_index = grid_url.find('~')
1474-
if grid_url[-1] == '/':
1475-
fid_in_url = grid_url[tilda_index + 1: -1].replace('/', ':')
1476-
else:
1477-
fid_in_url = grid_url[tilda_index + 1: len(grid_url)]
1478-
fid_in_url = fid_in_url.replace('/', ':')
1479-
get_url = upload_url + '/' + fid_in_url + '/content'
1473+
# extract path in grid url
1474+
url_path = six.moves.urllib.parse.urlparse(grid_url)[2][2:]
1475+
if url_path[-1] == '/':
1476+
url_path = url_path[0: -1]
1477+
url_path = url_path.replace('/', ':')
1478+
get_url = upload_url + '/' + url_path + '/content'
14801479

14811480
r = requests.get(get_url, headers=headers)
14821481
json_res = json.loads(r.text)
14831482
if raw is False:
1484-
# create a new grid
1485-
new_grid = grid_objs.Grid(
1486-
[grid_objs.Column(json_res['cols'][column]['data'], column)
1487-
for column in json_res['cols']]
1488-
)
1489-
# fill in uids
1490-
for column in new_grid:
1491-
column.id = json_res['cols'][column.name]['uid']
1492-
return new_grid
1483+
return Grid(json_res)
14931484
else:
14941485
return json_res
14951486

0 commit comments

Comments
 (0)