leaf_engine.io.gdrive.write

Functions

_setup_media(→ googleapiclient.http.MediaIoBaseUpload)

Creates MediaIoBaseUpload object with appropriate mime type.

create_folder(→ Optional[str])

If exist_ok, tries to find existing folder ID given name and

create_folders(→ Optional[str])

Creates folder tree from list of folder names in given parent folder. If no

to_drive(→ str)

Write a DataFrame to a file on Google Drive, at the specified path.

Module Contents

leaf_engine.io.gdrive.write._setup_media(df: pandas.DataFrame, path: str, **kwargs) googleapiclient.http.MediaIoBaseUpload

Creates MediaIoBaseUpload object with appropriate mime type.

Parameters:
Return type:

googleapiclient.http.MediaIoBaseUpload

leaf_engine.io.gdrive.write.create_folder(name: str, parent_folder_id: str | None, drive_id: str, exist_ok: bool = True) str | None

If exist_ok, tries to find existing folder ID given name and parent_folder_id. If no such folder exists or not exist_ok, creates the folder in the given parent (note that Google Drive allows multiple folders with same name in same parent).

Parameters:
  • name (str) – Folder name.

  • parent_folder_id (Optional[str]) – Where to put the folder. Parent folder ID can be found in the parent folder URL. If None, creates folder in Drive root.

  • exist_ok (bool) – Return existing folder ID, if available. Defaults to True.

  • drive_id (str) –

Returns:

Google Drive ID of created folder.

Return type:

str

leaf_engine.io.gdrive.write.create_folders(folders: List[str], drive_id: str, parent_folder_id: str | None = None, exist_ok: bool = True) str | None

Creates folder tree from list of folder names in given parent folder. If no parent folder provided, creates folder tree in root folder.

Parameters:
  • folders (List[str]) – List of folder names.

  • drive_id (str) – Google Drive ID.

  • parent_folder_id (str) – Folder ID of parent folder. Defaults to None.

  • exist_ok (bool, optional) – Existing folders can be re-used. Defaults to True.

Returns:

Google Drive ID of last folder in list.

Return type:

str

leaf_engine.io.gdrive.write.to_drive(df: pandas.DataFrame, path: str, overwrite: bool = False, **kwargs) str

Write a DataFrame to a file on Google Drive, at the specified path.

Parameters:
  • df (pd.DataFrame) – DataFrame to write.

  • path (str) – Path to write to. First part of path is the drive name.

  • overwrite (bool, optional) – Overwrite existing file. Defaults to False.

Return type:

str

Examples: >>> df.pipe(gdrive.to_drive, “drive_name/folder1/folder2/file_name.csv”) >>> df.pipe(gdrive.to_drive, “drive_name/folder1/folder2/file_name.xlsx”)

Raises:
  • LeafGoogleDriveException – Raised if overwrite is False and file exists.

  • LeafGoogleDriveException – Raised if path is invalid.

  • LeafGoogleDriveException – Raised if write response does not contain file ID.

Returns:

Google Drive file URL.

Return type:

str

Parameters: