Skip to content

Commit 587dfa5

Browse files
committed
Chris' comments sans tests
1 parent fca8fed commit 587dfa5

File tree

2 files changed

+126
-12
lines changed

2 files changed

+126
-12
lines changed

plotly/plotly/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
file_ops,
2424
get_config,
2525
get_grid,
26-
create_animations
26+
create_animations,
27+
icreate_animations
2728
)

plotly/plotly/plotly.py

+124-11
Original file line numberDiff line numberDiff line change
@@ -1493,40 +1493,153 @@ def get_grid(grid_url, raw=False):
14931493
return json_res
14941494

14951495

1496-
def create_animations(figure, filename=None, sharing='public'):
1496+
def create_animations(figure, filename=None, sharing='public', auto_open=True):
14971497
"""
1498-
Put description here.
1498+
BETA function that creates plots with animations via `frames`.
1499+
1500+
Creates an animated plot using 'frames' alongside 'data' and 'layout'.
1501+
This BETA endpoint is subject to deprecation in the future. In relation
1502+
to `plotly.plotly.plot`, folder-creation and overwriting are not supported
1503+
but creating a plot with or without animations via frames is supported.
1504+
1505+
:param (str) filename: if set to 'None', an automatically generated plot
1506+
name will be created. Does not support folder creation, meaning that
1507+
a folder of the form 'folder/name' will NOT create a the folder and
1508+
place the plot in it.
1509+
:param (str) sharing: see `plotly.plotly.plot()` doc string.
1510+
:param (bool) auto_open: if True, opens plot in the browser. If False,
1511+
returns the url for the plot instead.
1512+
1513+
Example 1: Simple Animation
1514+
```
1515+
import plotly.plotly as py
1516+
from plotly.grid_objs import Grid, Column
1517+
1518+
column_1 = Column([1, 2, 3], 'x')
1519+
column_2 = Column([1, 3, 6], 'y')
1520+
column_3 = Column([2, 4, 6], 'new x')
1521+
column_4 = Column([1, 1, 5], 'new y')
1522+
grid = Grid([column_1, column_2, column_3, column_4])
1523+
py.grid_ops.upload(grid, 'animations_grid', auto_open=False)
1524+
1525+
# create figure
1526+
figure = {
1527+
'data': [
1528+
{
1529+
'xsrc': grid.get_fid_uid('x'),
1530+
'ysrc': grid.get_fid_uid('y')
1531+
}
1532+
],
1533+
'layout': {'title': 'First Title'},
1534+
'frames': [
1535+
{
1536+
'data': [
1537+
{
1538+
'xsrc': grid.get_fid_uid('new x'),
1539+
'ysrc': grid.get_fid_uid('new y')
1540+
}
1541+
],
1542+
'layout': {'title': 'Second Title'}
1543+
}
1544+
]
1545+
}
14991546
1500-
For parameter descriptions, see the doc string for `py.plot()`.
1501-
`private` is not supported currently for param 'sharing'. Returns the
1502-
url for the plot if response is ok.
1547+
py.create_animations(figure, 'new_plot_with_animations')
1548+
```
15031549
"""
15041550
credentials = get_credentials()
15051551
validate_credentials(credentials)
15061552
username, api_key = credentials['username'], credentials['api_key']
15071553
auth = HTTPBasicAuth(str(username), str(api_key))
1508-
headers = {'Plotly-Client-Platform': 'python'}
1554+
headers = {'Plotly-Client-Platform': 'python',
1555+
'content-type': 'application/json'}
15091556

15101557
json = {
15111558
'figure': figure,
1512-
'world_readable': 'true'
1559+
'world_readable': True
15131560
}
15141561

15151562
# set filename if specified
15161563
if filename:
1564+
# warn user that creating folders isn't support in this version
1565+
if '/' in filename:
1566+
warnings.warn(
1567+
"This BETA version of 'create_animations' does not support "
1568+
"automatic folder creation. This means a filename of the form "
1569+
"'name1/name2' will just create the plot with that name only."
1570+
)
15171571
json['filename'] = filename
15181572

15191573
# set sharing
15201574
if sharing == 'public':
1521-
json['world_readable'] = 'true'
1575+
json['world_readable'] = True
15221576
elif sharing == 'private':
1523-
json['world_readable'] = 'false'
1577+
json['world_readable'] = False
1578+
elif sharing == 'secret':
1579+
json['world_readable'] = False
1580+
json['share_key_enabled'] = True
1581+
else:
1582+
raise exceptions.PlotlyError(
1583+
"Whoops, sharing can only be set to either 'public', 'private', "
1584+
"or 'secret'."
1585+
)
15241586

15251587
api_url = _api_v2.api_url('plots')
15261588
r = requests.post(api_url, auth=auth, headers=headers, json=json)
1589+
r.raise_for_status()
1590+
1591+
try:
1592+
parsed_response = r.json()
1593+
except:
1594+
parsed_response = r.content
1595+
1596+
if 'error' in r and r['error'] != '':
1597+
raise exceptions.PlotlyError(r['message'])
15271598

1528-
json_r = json.loads(r.text)
1529-
return json_r['file']['web_url']
1599+
if sharing == 'secret':
1600+
web_url = (parsed_response['file']['web_url'][:-1] +
1601+
'?share_key=' + parsed_response['file']['share_key'])
1602+
else:
1603+
web_url = parsed_response['file']['web_url']
1604+
1605+
if auto_open:
1606+
_open_url(web_url)
1607+
1608+
return web_url
1609+
1610+
1611+
def icreate_animations(figure, filename=None, sharing='public', auto_open=False):
1612+
"""
1613+
Create a unique url for this animated plot in Plotly and open in IPython.
1614+
1615+
This function is based off `plotly.plotly.iplot`. See `plotly.plotly.
1616+
create_animations` Doc String for param descriptions.
1617+
"""
1618+
url = create_animations(figure, filename, sharing, auto_open)
1619+
1620+
if isinstance(figure, dict):
1621+
layout = figure.get('layout', {})
1622+
else:
1623+
layout = {}
1624+
1625+
embed_options = dict()
1626+
embed_options['width'] = layout.get('width', '100%')
1627+
embed_options['height'] = layout.get('height', 525)
1628+
try:
1629+
float(embed_options['width'])
1630+
except (ValueError, TypeError):
1631+
pass
1632+
else:
1633+
embed_options['width'] = str(embed_options['width']) + 'px'
1634+
1635+
try:
1636+
float(embed_options['height'])
1637+
except (ValueError, TypeError):
1638+
pass
1639+
else:
1640+
embed_options['height'] = str(embed_options['height']) + 'px'
1641+
1642+
return tools.embed(url, **embed_options)
15301643

15311644

15321645
def _open_url(url):

0 commit comments

Comments
 (0)