@@ -1400,13 +1400,9 @@ def add_share_key_to_url(plot_url, attempt=0):
1400
1400
1401
1401
1402
1402
def _send_to_plotly (figure , ** plot_options ):
1403
- """
1404
-
1405
- """
1406
1403
fig = tools ._replace_newline (figure ) # does not mutate figure
1407
1404
data = json .dumps (fig ['data' ] if 'data' in fig else [],
1408
1405
cls = utils .PlotlyJSONEncoder )
1409
-
1410
1406
credentials = get_credentials ()
1411
1407
validate_credentials (credentials )
1412
1408
username = credentials ['username' ]
@@ -1427,14 +1423,15 @@ def _send_to_plotly(figure, **plot_options):
1427
1423
origin = 'plot' ,
1428
1424
kwargs = kwargs )
1429
1425
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"
1435
1430
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 )
1438
1435
1439
1436
if 'error' in r and r ['error' ] != '' :
1440
1437
raise exceptions .PlotlyError (r ['error' ])
@@ -1457,6 +1454,14 @@ def _send_to_plotly(figure, **plot_options):
1457
1454
1458
1455
1459
1456
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
+ """
1460
1465
url_v2_plot = "https://api.plot.ly/v2/plots"
1461
1466
url_v2_grid = "https://api.plot.ly/v2/grids"
1462
1467
auth = HTTPBasicAuth (str (payload ['un' ]), str (payload ['key' ]))
@@ -1467,7 +1472,7 @@ def create_animations(fig, kwargs, payload):
1467
1472
fig ['layout' ] = {}
1468
1473
1469
1474
# make a copy of fig
1470
- fig_copy = copy .deepcopy (fig )
1475
+ fig_with_uids = copy .deepcopy (fig )
1471
1476
1472
1477
# make grid
1473
1478
cols_dict = {}
@@ -1493,14 +1498,18 @@ def create_animations(fig, kwargs, payload):
1493
1498
for j in range (len (fig ['frames' ])):
1494
1499
for var in ['x' , 'y' ]:
1495
1500
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
1499
1506
}
1500
1507
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
1504
1513
}
1505
1514
counter += 1
1506
1515
trace_num += 1
@@ -1510,14 +1519,14 @@ def create_animations(fig, kwargs, payload):
1510
1519
"world_readable" : True
1511
1520
}
1512
1521
1513
- r = requests .post ('https://api.plot.ly/v2/grids' , auth = auth ,
1522
+ r = requests .post (url_v2_grid , auth = auth ,
1514
1523
headers = headers , json = grid_info )
1515
1524
r_dict = json .loads (r .text )
1516
1525
1517
1526
# make plot
1518
1527
fid = r_dict ['file' ]['fid' ]
1519
1528
cols_index = 0
1520
- for trace in fig_copy ['data' ]:
1529
+ for trace in fig_with_uids ['data' ]:
1521
1530
if 'x' in trace :
1522
1531
del trace ['x' ]
1523
1532
if 'y' in trace :
@@ -1533,27 +1542,38 @@ def create_animations(fig, kwargs, payload):
1533
1542
1534
1543
# replace data in frames by grid ids
1535
1544
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' ]
1540
1549
1541
- fig_copy ['frames' ][j ]['data' ][0 ]["xsrc" ] = "{fid}:{idlocal}" .format (
1550
+ fig_with_uids ['frames' ][j ]['data' ][0 ]["xsrc" ] = "{fid}:{idlocal}" .format (
1542
1551
fid = fid , idlocal = r_dict ['file' ]['cols' ][cols_index ]['uid' ]
1543
1552
)
1544
- fig_copy ['frames' ][j ]['data' ][0 ]["ysrc" ] = "{fid}:{idlocal}" .format (
1553
+ fig_with_uids ['frames' ][j ]['data' ][0 ]["ysrc" ] = "{fid}:{idlocal}" .format (
1545
1554
fid = fid , idlocal = r_dict ['file' ]['cols' ][cols_index + 1 ]['uid' ]
1546
1555
)
1547
1556
cols_index += 2
1548
1557
1549
1558
plots_info = {
1550
- "figure" : fig_copy ,
1551
- "world_readable" : True
1559
+ "figure" : fig_with_uids ,
1560
+ "world_readable" : json . loads ( kwargs )[ 'world_readable' ]
1552
1561
}
1553
1562
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
+ }
1556
1575
1576
+ return r_dict
1557
1577
1558
1578
1559
1579
def _open_url (url ):
0 commit comments