@@ -1356,12 +1356,10 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
1356
1356
"""
1357
1357
BETA function for uploading dashboards to Plotly.
1358
1358
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:
1365
1363
"""
1366
1364
if sharing == 'public' :
1367
1365
world_readable = True
@@ -1376,7 +1374,17 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
1376
1374
'world_readable' : world_readable
1377
1375
}
1378
1376
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 )
1380
1388
res .raise_for_status ()
1381
1389
1382
1390
url = res .json ()['web_url' ]
@@ -1406,7 +1414,7 @@ def _get_all_dashboards(cls):
1406
1414
return dashboards
1407
1415
1408
1416
@classmethod
1409
- def _get_dashboard_json (cls , dashboard_name ):
1417
+ def _get_dashboard_json (cls , dashboard_name , only_content = True ):
1410
1418
dashboards = cls ._get_all_dashboards ()
1411
1419
for index , dboard in enumerate (dashboards ):
1412
1420
if dboard ['filename' ] == dashboard_name :
@@ -1415,8 +1423,11 @@ def _get_dashboard_json(cls, dashboard_name):
1415
1423
dashboard = v2 .utils .request (
1416
1424
'get' , dashboards [index ]['api_urls' ]['dashboards' ]
1417
1425
).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
1420
1431
1421
1432
@classmethod
1422
1433
def get_dashboard (cls , dashboard_name ):
0 commit comments