@@ -209,9 +209,6 @@ def plot(figure_or_data, validate=True, **plot_options):
209
209
Make this figure private/public
210
210
211
211
"""
212
- #if 'frames' in figure_or_data:
213
- # figure = tools.return_figure_from_figure_or_data(figure_or_data, False)
214
- #else:
215
212
figure = tools .return_figure_from_figure_or_data (figure_or_data , validate )
216
213
for entry in figure ['data' ]:
217
214
if ('type' in entry ) and (entry ['type' ] == 'scattergl' ):
@@ -1410,8 +1407,8 @@ def _send_to_plotly(figure, **plot_options):
1410
1407
fileopt = plot_options ['fileopt' ],
1411
1408
world_readable = plot_options ['world_readable' ],
1412
1409
sharing = plot_options ['sharing' ],
1413
- layout = fig ['layout' ] if 'layout' in fig
1414
- else {}),
1410
+ layout = fig ['layout' ] if 'layout' in fig else {},
1411
+ frames = fig [ 'frames' ] if 'frames' in fig else {}),
1415
1412
cls = utils .PlotlyJSONEncoder )
1416
1413
1417
1414
# TODO: It'd be cool to expose the platform for RaspPi and others
@@ -1422,20 +1419,14 @@ def _send_to_plotly(figure, **plot_options):
1422
1419
key = api_key ,
1423
1420
origin = 'plot' ,
1424
1421
kwargs = kwargs )
1425
- print payload
1426
- print ' '
1427
1422
1428
- url = get_config ()['plotly_domain' ] + "/clientresp"
1423
+ if 'frames' in kwargs :
1424
+ r = create_animations (fig , kwargs , payload )
1429
1425
1430
- print url
1426
+ url = get_config ()[ 'plotly_domain' ] + "/clientresp"
1431
1427
1432
1428
r = requests .post (url , data = payload ,
1433
1429
verify = get_config ()['plotly_ssl_verification' ])
1434
- r .raise_for_status ()
1435
-
1436
- print r .text
1437
-
1438
- r = json .loads (r .text )
1439
1430
1440
1431
if 'error' in r and r ['error' ] != '' :
1441
1432
raise exceptions .PlotlyError (r ['error' ])
@@ -1457,6 +1448,76 @@ def _send_to_plotly(figure, **plot_options):
1457
1448
return r
1458
1449
1459
1450
1451
+ def create_animations (fig , kwargs , payload ):
1452
+ """
1453
+ The plan is to create a grid then a plot by deconstructing the fig if
1454
+ frames is found in the kwargs.
1455
+ """
1456
+ url_v2_plot = "https://api.plot.ly/v2/plots"
1457
+ url_v2_grid = "https://api.plot.ly/v2/grids"
1458
+ auth = HTTPBasicAuth (str (payload ['un' ]), str (payload ['key' ]))
1459
+ headers = {'Plotly-Client-Platform' : 'python' }
1460
+
1461
+ # add layout if not in fig
1462
+ if 'layout' not in fig :
1463
+ fig ['layout' ] = {}
1464
+
1465
+ # make grid
1466
+ cols_dict = {}
1467
+ counter = 0
1468
+ trace_num = 0
1469
+ for trace in fig ['data' ]:
1470
+ for var in ['x' , 'y' ]:
1471
+ if 'name' in trace :
1472
+ cols_dict ["{name}, {x_or_y}" .format (name = trace ['name' ],
1473
+ x_or_y = var )] = {
1474
+ "data" : list (trace [var ]), "order" : counter
1475
+ }
1476
+ else :
1477
+ cols_dict ["Trace {num}, {x_or_y}" .format (num = trace_num ,
1478
+ x_or_y = var )] = {
1479
+ "data" : list (trace [var ]), "order" : counter
1480
+ }
1481
+ counter += 1
1482
+ trace_num += 1
1483
+
1484
+ grid_info = {
1485
+ "data" : {"cols" : cols_dict },
1486
+ "world_readable" : True
1487
+ }
1488
+
1489
+ r = requests .post ('https://api.plot.ly/v2/grids' , auth = auth ,
1490
+ headers = headers , json = grid_info )
1491
+ r_dict = json .loads (r .text )
1492
+ # make a copy of the fig so that we not altering the original fig
1493
+ fig_copy = copy .deepcopy (fig )
1494
+
1495
+ # make plot
1496
+ fid = r_dict ['file' ]['fid' ]
1497
+ cols_index = 0
1498
+ for trace in fig_copy ['data' ]:
1499
+ if 'x' in trace :
1500
+ del trace ['x' ]
1501
+ if 'y' in trace :
1502
+ del trace ['y' ]
1503
+
1504
+ trace ["xsrc" ] = "{fid}:{idlocal}" .format (
1505
+ fid = fid , idlocal = r_dict ['file' ]['cols' ][cols_index ]['uid' ]
1506
+ )
1507
+ trace ["ysrc" ] = "{fid}:{idlocal}" .format (
1508
+ fid = fid , idlocal = r_dict ['file' ]['cols' ][cols_index + 1 ]['uid' ]
1509
+ )
1510
+ cols_index += 2
1511
+
1512
+ plots_info = {
1513
+ "figure" : fig_copy ,
1514
+ "world_readable" : True
1515
+ }
1516
+
1517
+ return requests .post ('https://api.plot.ly/v2/plots' , auth = auth ,
1518
+ headers = headers , json = plots_info )
1519
+
1520
+
1460
1521
def _open_url (url ):
1461
1522
try :
1462
1523
from webbrowser import open as wbopen
0 commit comments