Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 293f2bd

Browse files
committed
update upload function
1 parent 0c84e12 commit 293f2bd

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

plotly/api/v2/dashboards.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def retrieve(fid):
3232
def update(fid, content):
3333
"""Completely update the writable."""
3434
url = build_url(RESOURCE, id=fid)
35-
print url
3635
return request('put', url, json=content)
3736

3837

plotly/plotly/plotly.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,12 +1356,10 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
13561356
"""
13571357
BETA function for uploading dashboards to Plotly.
13581358
1359-
Functionality that we may need to consider adding:
1360-
- filename needs to be able to support `/` to create or use folders.
1361-
This'll require a few API calls.
1362-
- this function only works if the filename is unique. Need to call
1363-
`update` if this file already exists to overwrite the file.
1364-
- auto_open parameter for opening the result.
1359+
:param (dict) dashboard:
1360+
:param (str) filename:
1361+
:param (str) sharing:
1362+
:param (bool) auto_open:
13651363
"""
13661364
if sharing == 'public':
13671365
world_readable = True
@@ -1376,7 +1374,17 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
13761374
'world_readable': world_readable
13771375
}
13781376

1379-
res = v2.dashboards.create(data)
1377+
# check if pre-existing filename already exists
1378+
filenames = cls.get_dashboard_names()
1379+
if filename in filenames:
1380+
matching_dashboard = cls._get_dashboard_json(
1381+
filename, False
1382+
)
1383+
fid = matching_dashboard['fid']
1384+
res = v2.dashboards.update(fid, data)
1385+
1386+
else:
1387+
res = v2.dashboards.create(data)
13801388
res.raise_for_status()
13811389

13821390
url = res.json()['web_url']
@@ -1406,7 +1414,7 @@ def _get_all_dashboards(cls):
14061414
return dashboards
14071415

14081416
@classmethod
1409-
def _get_dashboard_json(cls, dashboard_name):
1417+
def _get_dashboard_json(cls, dashboard_name, only_content=True):
14101418
dashboards = cls._get_all_dashboards()
14111419
for index, dboard in enumerate(dashboards):
14121420
if dboard['filename'] == dashboard_name:
@@ -1415,8 +1423,11 @@ def _get_dashboard_json(cls, dashboard_name):
14151423
dashboard = v2.utils.request(
14161424
'get', dashboards[index]['api_urls']['dashboards']
14171425
).json()
1418-
dashboard_json = json.loads(dashboard['content'])
1419-
return dashboard_json
1426+
if only_content:
1427+
dashboard_json = json.loads(dashboard['content'])
1428+
return dashboard_json
1429+
else:
1430+
return dashboard
14201431

14211432
@classmethod
14221433
def get_dashboard(cls, dashboard_name):

0 commit comments

Comments
 (0)