A data scientist's toolbox for Google Suite Apps
Project description
pysuite: A data scientist's toolbox for Google Suite App
A python wrapper for google suite API. This provides a few classes with user friendly apis to operate with Google Suite applications such as Google Drive and Google Spreadsheet.
Get credentials
You need to get a credential from Google API Console. The credential looks like:
{
"installed": {
"client_id": "xxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"project_id": "xxxxxxxxxxxxx-xxxxxxxxxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "xxxxxxxxxxxxxxxx",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
You need to save this credential to a json file and pass to Authentication
class (For example, DriveAuth
).
In addition, you need to have a file to store refresh token. A pickle object will be written to the token file when
needed.
from pysuite import DriveAuth
credential_json_file = "/tmp/credential.json"
token_path_file = "/tmp/refresh_token.pickle"
client = DriveAuth(credential=credential_json_file, token=token_path_file)
Authenticate
Subclasses of Authentication
can help authenticate your credential and provide clients for API class such as Drive
and
Sheet
.
from pysuite import DriveAuth
credential_file = "./credentials/credentials.json"
token_file = "./credentials/token.pickle"
drive_auth = DriveAuth(credentials=credential_file, token_file=token_file)
this may prompt web browser confirmation for the first time if token_file is not created or is expired. Once you confirm access, the token will be created/overwritten.
You can generate a gdrive client now from authentication object.
service = drive_auth.get_service()
API
API classes aim to provide quick and simple access to Google Suite App such as Google Drive and Google Spreadsheet.
Drive
from pysuite import Drive
drive = Drive(service=drive_auth.get_service()) # drive_auth is an DriveAuth class
If you prefer different method to create gdrive client, you may switch drive_auth.get_service()
with a gdrive service
(See Google Drive API V3 for detail):
service = build('drive', 'v3', credentials=creds)
Some example apis are shown as follows:
Download file
drive.download(id="google drive object id", to_file="/tmp/test_file")
Upload file
drive.upload(from_file="path/to/your/file/to/be/uploaded", name="google_drive_file_name",
parent_ids=["google drive folder id 1", "google drive folder id 2"])
List file in folder
list_of_objects = drive.list(id="google drive folder id")
Sheet
from pysuite import Sheet
sheet = Sheet(service=sheet_auth.get_service()) # sheet_auth is an SheetAuth class
If you prefer different method to create gdrive client, you may switch sheet_auth.get_service()
with a gsheet service
(See Google Sheet API V4 for detail):
service = build('sheets', 'v4', credentials=creds, cache_discovery=True)
Some examples can be found below:
upload pandas dataframe
import pandas as pd
df = pd.DataFrame({"col1": [1, 2], "col2": ['a', 'b']})
sheet.to_sheet(df, id="your_sheet_id", range="yourtab!A1:B")
download sheet to dataframe
This api requires pandas.
df = sheet.read_sheet(id="your_sheet_id", range="yourtab!A1:D")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.