Skip to main content

A data scientist's toolbox for Google Suite Apps

Project description

pysuite: A data scientist's toolbox for Google Suite App

PyPi version PyPi downloads

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 can also provide a token json file if possible, the token file looks like:

{
     "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
     "refresh_token": "xxxxxxxxx"
}

If token file doesn't exist, a confirmation is needed from browser prompt. Then the token file will be created.

from pysuite import Authentication

credential_json_file = "/tmp/credential.json"
token_path_file = "/tmp/token.json"
client = Authentication(credential=credential_json_file, token=token_path_file, services="sheets")

Authenticate

Authentication blass can help authenticate your credential and provide service client for API. Such as "drive" and "sheets".

from pysuite import Authentication

credential_file = "./credentials/credentials.json"
token_file = "./credentials/token.json"

drive_auth = Authentication(credential=credential_file, token=token_file, services="drive")
sheets_auth = Authentication(credential=credential_file, token=token_file, services="sheets")

You can generate a gdrive client now from authentication object.

service = drive_auth.get_service_client()  # 'service' needed if not provided when initiating Authenciation object 

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_client())  # drive_auth is an Authenticaion class with `service='drive'`

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")

Sheets

from pysuite import Sheets

sheets = Sheets(service=sheets_auth.get_service_client())  # sheets_auth is an Authenticaion class with `service='sheets'`

If you prefer different method to create gdrive client, you may switch sheets_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']})
sheets.to_sheet(df, id="your_sheet_id", range="yourtab!A1:B")

download sheet to dataframe

This api requires pandas.

df = sheets.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.

Source Distribution

pysuite-0.2.1.tar.gz (8.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page