Download google_sheet.py from com3dian/hackathon: direct link, hf CLI and curl.
- Browser
- Download file 936 Bytes
-
https://huggingface.co/spaces/com3dian/hackathon/resolve/main/google_sheet.py
- Command line
-
hf download hf://spaces/com3dian/hackathon/google_sheet.py
-
curl -L -o google_sheet.py https://huggingface.co/spaces/com3dian/hackathon/resolve/main/google_sheet.py
936 Bytes
| # ======================== | |
| # Google Sheets Integration | |
| # ======================== | |
| import json | |
| import gspread | |
| import os | |
| import pandas as pd | |
| from oauth2client.service_account import ServiceAccountCredentials | |
| def connect_to_sheet(): | |
| creds_dict = json.loads(os.environ["GOOGLE_CREDS_JSON"]) | |
| scope = [ | |
| "https://spreadsheets.google.com/feeds", | |
| "https://www.googleapis.com/auth/spreadsheets", | |
| "https://www.googleapis.com/auth/drive" | |
| ] | |
| creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope) | |
| client = gspread.authorize(creds) | |
| sheet = client.open_by_key("109h0Yj8RmKEbWCj5TdJNXXbWhmaVH8o3r3q4_Pa50g8").sheet1 | |
| return sheet | |
| def append_score(timestamp, score, filename): | |
| sheet = connect_to_sheet() | |
| sheet.append_row([timestamp, score, filename]) | |
| def fetch_leaderboard(): | |
| sheet = connect_to_sheet() | |
| data = sheet.get_all_records() | |
| return pd.DataFrame(data) |