Created
May 10, 2020 14:01
-
-
Save realpacific/52cd409e8ddc19668858418d8948d16a to your computer and use it in GitHub Desktop.
Python Google Sheets as backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import json | |
import time | |
import os | |
from flask import Flask | |
from googleapiclient.discovery import build | |
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'] | |
SAMPLE_SPREADSHEET_ID = os.environ['SPREADSHEET_ID'] | |
SAMPLE_RANGE_NAME = 'A:D' | |
service = build('sheets', 'v4', developerKey=os.environ['KEY']) | |
sheet = service.spreadsheets() | |
def main(): | |
start = time.time() | |
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME).execute() | |
values = result.get('values', []) | |
res = [] | |
if not values: | |
print('No data found.') | |
else: | |
print(values) | |
keys = values[0] | |
for row in values[1:]: | |
now = {} | |
for r, k in zip(row, keys): | |
now[k] = r | |
res.append(now) | |
print(res) | |
print(time.time() - start) | |
return res | |
app = Flask(__name__) | |
@app.route('/', methods=['GET']) | |
def homepage(): | |
return json.dumps(main()) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment