|
47 | 47 | 'sharing': files.FILE_CONTENT[files.CONFIG_FILE]['sharing']
|
48 | 48 | }
|
49 | 49 |
|
| 50 | +SHARING_ERROR_MSG = ( |
| 51 | + "Whoops, sharing can only be set to either 'public', 'private', or " |
| 52 | + "'secret'." |
| 53 | +) |
| 54 | + |
50 | 55 | # test file permissions and make sure nothing is corrupted
|
51 | 56 | tools.ensure_local_plotly_files()
|
52 | 57 |
|
@@ -1520,6 +1525,81 @@ def get_dashboard_names(cls):
|
1520 | 1525 | return [str(dboard['filename']) for dboard in dashboards]
|
1521 | 1526 |
|
1522 | 1527 |
|
| 1528 | +class presentation_ops: |
| 1529 | + """ |
| 1530 | + Interface to Plotly's Spectacle-Presentations API. |
| 1531 | + """ |
| 1532 | + @classmethod |
| 1533 | + def upload(cls, presentation, filename, sharing='public', auto_open=True): |
| 1534 | + """ |
| 1535 | + Function for uploading presentations to Plotly. |
| 1536 | +
|
| 1537 | + :param (dict) presentation: the JSON presentation to be uploaded. Use |
| 1538 | + plotly.presentation_objs.Presentation to create presentations |
| 1539 | + from a Markdown-like string. |
| 1540 | + :param (str) filename: the name of the presentation to be saved in |
| 1541 | + your Plotly account. Will overwrite a presentation of the same |
| 1542 | + name if it already exists in your files. |
| 1543 | + :param (str) sharing: can be set to either 'public', 'private' |
| 1544 | + or 'secret'. If 'public', your presentation will be viewable by |
| 1545 | + all other users. If 'private' only you can see your presentation. |
| 1546 | + If it is set to 'secret', the url will be returned with a string |
| 1547 | + of random characters appended to the url which is called a |
| 1548 | + sharekey. The point of a sharekey is that it makes the url very |
| 1549 | + hard to guess, but anyone with the url can view the presentation. |
| 1550 | + :param (bool) auto_open: automatically opens the presentation in the |
| 1551 | + browser. |
| 1552 | +
|
| 1553 | + See the documentation online for examples. |
| 1554 | + """ |
| 1555 | + if sharing == 'public': |
| 1556 | + world_readable = True |
| 1557 | + elif sharing in ['private', 'secret']: |
| 1558 | + world_readable = False |
| 1559 | + else: |
| 1560 | + raise exceptions.PlotlyError( |
| 1561 | + SHARING_ERROR_MSG |
| 1562 | + ) |
| 1563 | + data = { |
| 1564 | + 'content': json.dumps(presentation), |
| 1565 | + 'filename': filename, |
| 1566 | + 'world_readable': world_readable |
| 1567 | + } |
| 1568 | + |
| 1569 | + # lookup if pre-existing filename already exists |
| 1570 | + try: |
| 1571 | + lookup_res = v2.files.lookup(filename) |
| 1572 | + lookup_res.raise_for_status() |
| 1573 | + matching_file = json.loads(lookup_res.content) |
| 1574 | + |
| 1575 | + if matching_file['filetype'] != 'spectacle_presentation': |
| 1576 | + raise exceptions.PlotlyError( |
| 1577 | + "'{filename}' is already a {filetype} in your account. " |
| 1578 | + "You can't overwrite a file that is not a spectacle_" |
| 1579 | + "presentation. Please pick another filename.".format( |
| 1580 | + filename=filename, |
| 1581 | + filetype=matching_file['filetype'] |
| 1582 | + ) |
| 1583 | + ) |
| 1584 | + else: |
| 1585 | + old_fid = matching_file['fid'] |
| 1586 | + res = v2.spectacle_presentations.update(old_fid, data) |
| 1587 | + |
| 1588 | + except exceptions.PlotlyRequestError: |
| 1589 | + res = v2.spectacle_presentations.create(data) |
| 1590 | + res.raise_for_status() |
| 1591 | + |
| 1592 | + url = res.json()['web_url'] |
| 1593 | + |
| 1594 | + if sharing == 'secret': |
| 1595 | + url = add_share_key_to_url(url) |
| 1596 | + |
| 1597 | + if auto_open: |
| 1598 | + webbrowser.open_new(res.json()['web_url']) |
| 1599 | + |
| 1600 | + return url |
| 1601 | + |
| 1602 | + |
1523 | 1603 | def create_animations(figure, filename=None, sharing='public', auto_open=True):
|
1524 | 1604 | """
|
1525 | 1605 | BETA function that creates plots with animations via `frames`.
|
@@ -1712,8 +1792,7 @@ def create_animations(figure, filename=None, sharing='public', auto_open=True):
|
1712 | 1792 | body['share_key_enabled'] = True
|
1713 | 1793 | else:
|
1714 | 1794 | raise exceptions.PlotlyError(
|
1715 |
| - "Whoops, sharing can only be set to either 'public', 'private', " |
1716 |
| - "or 'secret'." |
| 1795 | + SHARING_ERROR_MSG |
1717 | 1796 | )
|
1718 | 1797 |
|
1719 | 1798 | response = v2.plots.create(body)
|
|
0 commit comments