@@ -628,38 +628,35 @@ def save_as(cls, figure_or_data, filename, format=None, width=None, height=None)
628
628
f .write (img )
629
629
f .close ()
630
630
631
+
631
632
class file_ops :
632
633
""" Interface to Plotly's File System API
633
634
"""
634
635
635
636
@classmethod
636
- def mkdir (cls , folder_path ):
637
+ def mkdirs (cls , folder_path ):
637
638
""" 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
+
642
646
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')
645
650
"""
646
651
647
- # trim trailing slash
652
+ # trim trailing slash TODO: necessesary?
648
653
if folder_path [- 1 ] == '/' :
649
654
folder_path = folder_path [0 :- 1 ]
650
655
651
- folder_names = folder_path .split ('/' )
652
- name = folder_names [- 1 ]
653
656
payload = {
654
- 'name ' : name
657
+ 'path ' : folder_path
655
658
}
656
659
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
-
663
660
url = _api_v2 .api_url ('folders' )
664
661
665
662
res = requests .post (url , data = payload , headers = _api_v2 .headers ())
@@ -684,9 +681,19 @@ def _fill_in_response_column_ids(cls, request_columns,
684
681
@classmethod
685
682
def upload (cls , grid , filename , world_readable = True , auto_open = True , meta = None ):
686
683
""" Upload a grid to your Plotly account with the specified filename.
687
-
688
684
"""
689
685
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
+
690
697
# transmorgify grid object into plotly's format
691
698
grid_json = {'cols' : {}}
692
699
if meta is not None :
@@ -704,8 +711,13 @@ def upload(cls, grid, filename, world_readable=True, auto_open=True, meta=None):
704
711
'world_readable' : world_readable
705
712
}
706
713
714
+ if parent_path != '' :
715
+ payload ['parent_path' ] = parent_path
716
+
707
717
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
+
709
721
res = _api_v2 .response_handler (req )
710
722
711
723
response_columns = res ['file' ]['cols' ]
0 commit comments