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 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
File details
Details for the file pysuite-0.2.0.tar.gz
.
File metadata
- Download URL: pysuite-0.2.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ee8f9f0af92223d7f990d015379d895f1acf4406bb0e58968695ab81c98dc9e |
|
MD5 | d9cf81445742948b4cf8bbce5e4d5ca4 |
|
BLAKE2b-256 | b323624fbde2941ba85d9af515fae0c96491af50df02a463fd8690eefc509ef2 |