Skip to content

Commit d17a222

Browse files
committed
added function to spit out uids from column names
1 parent d173cbb commit d17a222

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ def __init__(self, *args, **kwargs):
836836
_parent_key='data')
837837
figure_class.__init__ = __init__
838838

839+
# TODO better integrate frames into Figure - #604
839840
def __setitem__(self, key, value, **kwargs):
840841
if key == 'frames':
841842
super(PlotlyDict, self).__setitem__(key, value)
@@ -845,6 +846,7 @@ def __setitem__(self, key, value, **kwargs):
845846

846847
def _get_valid_attributes(self):
847848
super(figure_class, self)._get_valid_attributes()
849+
# TODO better integrate frames into Figure - #604
848850
if 'frames' not in self._valid_attributes:
849851
self._valid_attributes.add('frames')
850852
return self._valid_attributes

plotly/offline/offline.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def init_notebook_mode(connected=False):
163163
def _plot_html(figure_or_data, config, validate, default_width,
164164
default_height, global_requirejs):
165165
# force no validation if frames is in the call
166+
# TODO - add validation for frames in call - #
166167
if 'frames' in figure_or_data:
167168
figure = tools.return_figure_from_figure_or_data(
168169
figure_or_data, False
@@ -196,25 +197,26 @@ def _plot_html(figure_or_data, config, validate, default_width,
196197
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder)
197198

198199
configkeys = (
199-
'editable',
200-
'autosizable',
201-
'fillFrame',
202-
'frameMargins',
203-
'scrollZoom',
204-
'doubleClick',
205-
'showTips',
206-
'showLink',
207-
'sendData',
208-
'linkText',
209-
'showSources',
210-
'displayModeBar',
211-
'modeBarButtonsToRemove',
212-
'modeBarButtonsToAdd',
213-
'modeBarButtons',
214-
'displaylogo',
215-
'plotGlPixelRatio',
216-
'setBackground',
217-
'topojsonURL')
200+
'editable',
201+
'autosizable',
202+
'fillFrame',
203+
'frameMargins',
204+
'scrollZoom',
205+
'doubleClick',
206+
'showTips',
207+
'showLink',
208+
'sendData',
209+
'linkText',
210+
'showSources',
211+
'displayModeBar',
212+
'modeBarButtonsToRemove',
213+
'modeBarButtonsToAdd',
214+
'modeBarButtons',
215+
'displaylogo',
216+
'plotGlPixelRatio',
217+
'setBackground',
218+
'topojsonURL'
219+
)
218220

219221
config_clean = dict((k,config[k]) for k in configkeys if k in config)
220222

plotly/plotly/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
meta_ops,
2323
file_ops,
2424
get_config,
25-
bad_create_animations
25+
bad_create_animations,
26+
get_uid_by_col_name
2627
)

plotly/plotly/plotly.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def heartbeat(self, reconnect_on=(200, '', 408)):
499499
"cannot write to a closed connection. "
500500
"Call `open()` on the stream to open the stream."
501501
)
502-
502+
503503
@property
504504
def connected(self):
505505
if self._stream is None:
@@ -939,6 +939,7 @@ def upload(cls, grid, filename,
939939

940940
paths = filename.split('/')
941941
parent_path = '/'.join(paths[0:-1])
942+
942943
filename = paths[-1]
943944

944945
if parent_path != '':
@@ -959,6 +960,7 @@ def upload(cls, grid, filename,
959960
payload['parent_path'] = parent_path
960961

961962
upload_url = _api_v2.api_url('grids')
963+
962964
req = requests.post(upload_url, data=payload,
963965
headers=_api_v2.headers(),
964966
verify=get_config()['plotly_ssl_verification'])
@@ -1423,9 +1425,6 @@ def _send_to_plotly(figure, **plot_options):
14231425
origin='plot',
14241426
kwargs=kwargs)
14251427

1426-
#if 'frames' in fig:
1427-
# r = create_animations(fig, kwargs, payload)
1428-
#else:
14291428
url = get_config()['plotly_domain'] + "/clientresp"
14301429

14311430
r = requests.post(url, data=payload,
@@ -1576,6 +1575,39 @@ def bad_create_animations(fig, kwargs, payload):
15761575
return r_dict
15771576

15781577

1578+
def get_uid_by_col_name(grid_url, col_name):
1579+
"""
1580+
Search for a column of a grid by name and return the uid of the column.
1581+
"""
1582+
credentials = get_credentials()
1583+
validate_credentials(credentials)
1584+
auth = HTTPBasicAuth(credentials['username'], credentials['api_key'])
1585+
headers = {'Plotly-Client-Platform': 'python'}
1586+
upload_url = _api_v2.api_url('grids')
1587+
1588+
tilda_index = grid_url.find('~')
1589+
fid_in_url = grid_url[tilda_index + 1: -1].replace('/', ':')
1590+
get_url = upload_url + '/' + fid_in_url
1591+
1592+
r = requests.get(get_url, auth=auth, headers=headers)
1593+
r_text = json.loads(r.text)
1594+
1595+
col_uid = ''
1596+
for col in r_text['cols']:
1597+
if col_name == col['name']:
1598+
col_uid = col['uid']
1599+
break
1600+
1601+
all_col_names = ([r_text['cols'][j]['name'] for j in
1602+
range(len(r_text['cols']))])
1603+
if col_uid == '':
1604+
raise exceptions.PlotlyError(
1605+
'The col_name does not match with any column name in your grid. '
1606+
'The column names in your grid are {}'.format(all_col_names))
1607+
else:
1608+
return col_uid
1609+
1610+
15791611
def _open_url(url):
15801612
try:
15811613
from webbrowser import open as wbopen

0 commit comments

Comments
 (0)