Skip to content

Instantly share code, notes, and snippets.

@wescpy
Last active November 24, 2021 19:57
Show Gist options
  • Save wescpy/3d0224bdcde4bf4059b0432e6b50dca6 to your computer and use it in GitHub Desktop.
Save wescpy/3d0224bdcde4bf4059b0432e6b50dca6 to your computer and use it in GitHub Desktop.
Demo app showing how to call (non-Cloud) Google APIs from GCP serverless platforms
# Copyright 2021 Google LLC
# SPDX-License-Identifier: Apache-2.0
from flask import Flask, render_template
import google.auth
from googleapiclient import discovery
app = Flask(__name__)
CREDS, _PROJECT_ID = google.auth.default()
SHEETS = discovery.build('sheets', 'v4', credentials=CREDS)
SHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
@app.route('/')
def students(_req=None):
"""
read spreadsheet data and render in template
"""
# read data from Google Sheet
rows = SHEETS.spreadsheets().values().get(spreadsheetId=SHEET_ID,
range='Class Data',
fields='values'
).execute().get('values', [])
# create context & render template
context = {'headers': rows[0], 'students': rows[1:]}
return render_template('index.html', **context)
if __name__ == '__main__':
import os
app.run(debug=True, threaded=True, host='0.0.0.0',
port=int(os.environ.get('PORT', 8080)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment