Skip to content

Commit a156503

Browse files
committed
added create_animations so v2 is hit if frames is in fig
1 parent b2b8451 commit a156503

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

plotly/plotly/plotly.py

+50-30
Original file line numberDiff line numberDiff line change
@@ -1400,13 +1400,9 @@ def add_share_key_to_url(plot_url, attempt=0):
14001400

14011401

14021402
def _send_to_plotly(figure, **plot_options):
1403-
"""
1404-
1405-
"""
14061403
fig = tools._replace_newline(figure) # does not mutate figure
14071404
data = json.dumps(fig['data'] if 'data' in fig else [],
14081405
cls=utils.PlotlyJSONEncoder)
1409-
14101406
credentials = get_credentials()
14111407
validate_credentials(credentials)
14121408
username = credentials['username']
@@ -1427,14 +1423,15 @@ def _send_to_plotly(figure, **plot_options):
14271423
origin='plot',
14281424
kwargs=kwargs)
14291425

1430-
print payload
1431-
#if 'frames' in fig:
1432-
# r = create_animations(fig, kwargs, payload)
1433-
1434-
url = get_config()['plotly_domain'] + "/clientresp"
1426+
if 'frames' in fig:
1427+
r = create_animations(fig, kwargs, payload)
1428+
else:
1429+
url = get_config()['plotly_domain'] + "/clientresp"
14351430

1436-
r = requests.post(url, data=payload,
1437-
verify=get_config()['plotly_ssl_verification'])
1431+
r = requests.post(url, data=payload,
1432+
verify=get_config()['plotly_ssl_verification'])
1433+
r.raise_for_status()
1434+
r = json.loads(r.text)
14381435

14391436
if 'error' in r and r['error'] != '':
14401437
raise exceptions.PlotlyError(r['error'])
@@ -1457,6 +1454,14 @@ def _send_to_plotly(figure, **plot_options):
14571454

14581455

14591456
def create_animations(fig, kwargs, payload):
1457+
"""
1458+
Makes a post to GRIDS and PLOTS if frames is in the figure.
1459+
1460+
This function bypasses the current '/clientresp' route of making a POST
1461+
request to return back a url. Instead, the V2 REST API is used if frames
1462+
is part of the figure's keys. Currently, 'error', 'message' and 'warning'
1463+
are hard codded to the empty string.
1464+
"""
14601465
url_v2_plot = "https://api.plot.ly/v2/plots"
14611466
url_v2_grid = "https://api.plot.ly/v2/grids"
14621467
auth = HTTPBasicAuth(str(payload['un']), str(payload['key']))
@@ -1467,7 +1472,7 @@ def create_animations(fig, kwargs, payload):
14671472
fig['layout'] = {}
14681473

14691474
# make a copy of fig
1470-
fig_copy = copy.deepcopy(fig)
1475+
fig_with_uids = copy.deepcopy(fig)
14711476

14721477
# make grid
14731478
cols_dict = {}
@@ -1493,14 +1498,18 @@ def create_animations(fig, kwargs, payload):
14931498
for j in range(len(fig['frames'])):
14941499
for var in ['x', 'y']:
14951500
if 'name' in fig['frames'][j]['data']:
1496-
cols_dict["{name}, {x_or_y}".format(name=fig['frames'][j]['data'][0]['name'],
1497-
x_or_y=var)] = {
1498-
"data": list(fig['frames'][j]['data'][0][var]), "order": counter
1501+
cols_dict["{name}, {x_or_y}".format(
1502+
name=fig['frames'][j]['data'][0]['name'], x_or_y=var
1503+
)] = {
1504+
"data": list(fig['frames'][j]['data'][0][var]),
1505+
"order": counter
14991506
}
15001507
else:
1501-
cols_dict["Trace {num}, {x_or_y}".format(num=trace_num,
1502-
x_or_y=var)] = {
1503-
"data": list(fig['frames'][j]['data'][0][var]), "order": counter
1508+
cols_dict["Trace {num}, {x_or_y}".format(
1509+
num=trace_num, x_or_y=var
1510+
)] = {
1511+
"data": list(fig['frames'][j]['data'][0][var]),
1512+
"order": counter
15041513
}
15051514
counter += 1
15061515
trace_num += 1
@@ -1510,14 +1519,14 @@ def create_animations(fig, kwargs, payload):
15101519
"world_readable": True
15111520
}
15121521

1513-
r = requests.post('https://api.plot.ly/v2/grids', auth=auth,
1522+
r = requests.post(url_v2_grid, auth=auth,
15141523
headers=headers, json=grid_info)
15151524
r_dict = json.loads(r.text)
15161525

15171526
# make plot
15181527
fid = r_dict['file']['fid']
15191528
cols_index = 0
1520-
for trace in fig_copy['data']:
1529+
for trace in fig_with_uids['data']:
15211530
if 'x' in trace:
15221531
del trace['x']
15231532
if 'y' in trace:
@@ -1533,27 +1542,38 @@ def create_animations(fig, kwargs, payload):
15331542

15341543
# replace data in frames by grid ids
15351544
for j in range(len(fig['frames'])):
1536-
if 'x' in fig_copy['frames'][j]['data'][0]:
1537-
del fig_copy['frames'][j]['data'][0]['x']
1538-
if 'y' in fig_copy['frames'][j]['data'][0]:
1539-
del fig_copy['frames'][j]['data'][0]['y']
1545+
if 'x' in fig_with_uids['frames'][j]['data'][0]:
1546+
del fig_with_uids['frames'][j]['data'][0]['x']
1547+
if 'y' in fig_with_uids['frames'][j]['data'][0]:
1548+
del fig_with_uids['frames'][j]['data'][0]['y']
15401549

1541-
fig_copy['frames'][j]['data'][0]["xsrc"] = "{fid}:{idlocal}".format(
1550+
fig_with_uids['frames'][j]['data'][0]["xsrc"] = "{fid}:{idlocal}".format(
15421551
fid=fid, idlocal=r_dict['file']['cols'][cols_index]['uid']
15431552
)
1544-
fig_copy['frames'][j]['data'][0]["ysrc"] = "{fid}:{idlocal}".format(
1553+
fig_with_uids['frames'][j]['data'][0]["ysrc"] = "{fid}:{idlocal}".format(
15451554
fid=fid, idlocal=r_dict['file']['cols'][cols_index + 1]['uid']
15461555
)
15471556
cols_index += 2
15481557

15491558
plots_info = {
1550-
"figure": fig_copy,
1551-
"world_readable": True
1559+
"figure": fig_with_uids,
1560+
"world_readable": json.loads(kwargs)['world_readable']
15521561
}
15531562

1554-
requests.post('https://api.plot.ly/v2/plots', auth=auth,
1555-
headers=headers, json=plots_info)
1563+
r = requests.post(url_v2_plot, auth=auth,
1564+
headers=headers, json=plots_info)
1565+
1566+
r_json = json.loads(r.text)
1567+
1568+
r_dict = {
1569+
'error': '',
1570+
'filename': json.loads(kwargs)['filename'],
1571+
'message': '',
1572+
'url': r_json['file']['web_url'],
1573+
'warning': ''
1574+
}
15561575

1576+
return r_dict
15571577

15581578

15591579
def _open_url(url):

0 commit comments

Comments
 (0)