Skip to content

Commit ded8a37

Browse files
committed
make folder if it doesnt exist
1 parent f1e600b commit ded8a37

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

plotly/plotly/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"""
1010
from . plotly import (
1111
sign_in, update_plot_options, get_plot_options, get_credentials, iplot,
12-
plot, iplot_mpl, plot_mpl, get_figure, Stream, image, grid_ops
12+
plot, iplot_mpl, plot_mpl, get_figure, Stream, image, grid_ops,
13+
meta_ops, file_ops, _api_v2
1314
)

plotly/plotly/plotly.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -628,38 +628,35 @@ def save_as(cls, figure_or_data, filename, format=None, width=None, height=None)
628628
f.write(img)
629629
f.close()
630630

631+
631632
class file_ops:
632633
""" Interface to Plotly's File System API
633634
"""
634635

635636
@classmethod
636-
def mkdir(cls, folder_path):
637+
def mkdirs(cls, folder_path):
637638
""" Make a folder in Plotly at folder_path
638-
Mimics the shell's mkdir.
639-
Exceptions:
640-
- Parent folder doesn't exist
641-
- A file already exists with that same name
639+
Mimics the shell's mkdir -p.
640+
Returns:
641+
- 200 if folders already existed, nothing was created
642+
- 201 if path was created
643+
Raises:
644+
- requests.exceptions.RequestException: 400
645+
642646
Usage examples:
643-
>> mkdir('new folder')
644-
>> mkdir('existing folder/new folder')
647+
>> mkdirs('new folder')
648+
>> mkdirs('existing folder/new folder')
649+
>> mkdirs('new/folder/path')
645650
"""
646651

647-
# trim trailing slash
652+
# trim trailing slash TODO: necessesary?
648653
if folder_path[-1] == '/':
649654
folder_path = folder_path[0:-1]
650655

651-
folder_names = folder_path.split('/')
652-
name = folder_names[-1]
653656
payload = {
654-
'name': name
657+
'path': folder_path
655658
}
656659

657-
parent_path = '/'.join(folder_names[0:-1])
658-
if parent_path == '':
659-
payload['parent'] = -1
660-
else:
661-
payload['parent_path'] = parent_path
662-
663660
url = _api_v2.api_url('folders')
664661

665662
res = requests.post(url, data=payload, headers=_api_v2.headers())
@@ -684,9 +681,19 @@ def _fill_in_response_column_ids(cls, request_columns,
684681
@classmethod
685682
def upload(cls, grid, filename, world_readable=True, auto_open=True, meta=None):
686683
""" Upload a grid to your Plotly account with the specified filename.
687-
688684
"""
689685

686+
# Made a folder path
687+
if filename[-1] == '/':
688+
filename = filename[0:-1]
689+
690+
paths = filename.split('/')
691+
parent_path = '/'.join(paths[0:-1])
692+
filename = paths[-1]
693+
694+
if parent_path != '':
695+
file_ops.mkdirs(parent_path)
696+
690697
# transmorgify grid object into plotly's format
691698
grid_json = {'cols': {}}
692699
if meta is not None:
@@ -704,8 +711,13 @@ def upload(cls, grid, filename, world_readable=True, auto_open=True, meta=None):
704711
'world_readable': world_readable
705712
}
706713

714+
if parent_path != '':
715+
payload['parent_path'] = parent_path
716+
707717
upload_url = _api_v2.api_url('grids')
708-
req = requests.post(upload_url, data=payload, headers=_api_v2.headers())
718+
req = requests.post(upload_url, data=payload,
719+
headers=_api_v2.headers())
720+
709721
res = _api_v2.response_handler(req)
710722

711723
response_columns = res['file']['cols']

0 commit comments

Comments
 (0)