Skip to content

Commit efa390e

Browse files
committed
__init__.py now accepts create_animations which lives in plotly.py -- started work on generating grid/plot in v2 in create_animations
1 parent d19c6ca commit efa390e

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

plotly/plotly/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
grid_ops,
2222
meta_ops,
2323
file_ops,
24-
get_config
24+
get_config,
25+
create_animations
2526
)

plotly/plotly/plotly.py

+75-14
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ def plot(figure_or_data, validate=True, **plot_options):
209209
Make this figure private/public
210210
211211
"""
212-
#if 'frames' in figure_or_data:
213-
# figure = tools.return_figure_from_figure_or_data(figure_or_data, False)
214-
#else:
215212
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
216213
for entry in figure['data']:
217214
if ('type' in entry) and (entry['type'] == 'scattergl'):
@@ -1410,8 +1407,8 @@ def _send_to_plotly(figure, **plot_options):
14101407
fileopt=plot_options['fileopt'],
14111408
world_readable=plot_options['world_readable'],
14121409
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 {}),
14151412
cls=utils.PlotlyJSONEncoder)
14161413

14171414
# TODO: It'd be cool to expose the platform for RaspPi and others
@@ -1422,20 +1419,14 @@ def _send_to_plotly(figure, **plot_options):
14221419
key=api_key,
14231420
origin='plot',
14241421
kwargs=kwargs)
1425-
print payload
1426-
print ' '
14271422

1428-
url = get_config()['plotly_domain'] + "/clientresp"
1423+
if 'frames' in kwargs:
1424+
r = create_animations(fig, kwargs, payload)
14291425

1430-
print url
1426+
url = get_config()['plotly_domain'] + "/clientresp"
14311427

14321428
r = requests.post(url, data=payload,
14331429
verify=get_config()['plotly_ssl_verification'])
1434-
r.raise_for_status()
1435-
1436-
print r.text
1437-
1438-
r = json.loads(r.text)
14391430

14401431
if 'error' in r and r['error'] != '':
14411432
raise exceptions.PlotlyError(r['error'])
@@ -1457,6 +1448,76 @@ def _send_to_plotly(figure, **plot_options):
14571448
return r
14581449

14591450

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+
14601521
def _open_url(url):
14611522
try:
14621523
from webbrowser import open as wbopen

0 commit comments

Comments
 (0)