Spaces:
Runtime error
Runtime error
Fetch questions and answers from google sheets
#2
by
dongwook-chan
- opened
ice_breaking_challenge/google_sheets.py
CHANGED
|
@@ -5,6 +5,7 @@ from google.oauth2.credentials import Credentials
|
|
| 5 |
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 6 |
from googleapiclient.discovery import build
|
| 7 |
from googleapiclient.errors import HttpError
|
|
|
|
| 8 |
|
| 9 |
# If modifying these scopes, delete the file token.json.
|
| 10 |
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
|
|
@@ -12,10 +13,11 @@ SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
|
|
| 12 |
# The ID and range of a sample spreadsheet.
|
| 13 |
# SAMPLE_SPREADSHEET_ID = "1Z3doUDpoUmXs7p4-g7f8q3XZIBhH_Lz2w51bnpUFigw"
|
| 14 |
SAMPLE_SPREADSHEET_ID = "1NJGhpMsfUySHJcMBNpLTEnt-r5Co6qwRahenIa9D55k"
|
| 15 |
-
SAMPLE_RANGE_NAME = "Responses!I2:I30"
|
| 16 |
|
| 17 |
SERVICE = None
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def load_google_sheets():
|
| 21 |
global SERVICE
|
|
@@ -43,20 +45,37 @@ def load_google_sheets():
|
|
| 43 |
|
| 44 |
SERVICE = build("sheets", "v4", credentials=creds)
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
sheet = SERVICE.spreadsheets()
|
| 48 |
result = (
|
| 49 |
sheet.values()
|
| 50 |
.get(
|
| 51 |
spreadsheetId=SAMPLE_SPREADSHEET_ID,
|
| 52 |
-
range=
|
| 53 |
)
|
| 54 |
.execute()
|
| 55 |
)
|
| 56 |
values = result.get("values", [])
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
return [value[0] for value in values]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 6 |
from googleapiclient.discovery import build
|
| 7 |
from googleapiclient.errors import HttpError
|
| 8 |
+
from flask import session
|
| 9 |
|
| 10 |
# If modifying these scopes, delete the file token.json.
|
| 11 |
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
|
|
|
|
| 13 |
# The ID and range of a sample spreadsheet.
|
| 14 |
# SAMPLE_SPREADSHEET_ID = "1Z3doUDpoUmXs7p4-g7f8q3XZIBhH_Lz2w51bnpUFigw"
|
| 15 |
SAMPLE_SPREADSHEET_ID = "1NJGhpMsfUySHJcMBNpLTEnt-r5Co6qwRahenIa9D55k"
|
|
|
|
| 16 |
|
| 17 |
SERVICE = None
|
| 18 |
|
| 19 |
+
MAX_ROW = 100
|
| 20 |
+
|
| 21 |
|
| 22 |
def load_google_sheets():
|
| 23 |
global SERVICE
|
|
|
|
| 45 |
|
| 46 |
SERVICE = build("sheets", "v4", credentials=creds)
|
| 47 |
|
| 48 |
+
|
| 49 |
+
def get_range(range_name: str) -> list[list[str]]:
|
| 50 |
sheet = SERVICE.spreadsheets()
|
| 51 |
result = (
|
| 52 |
sheet.values()
|
| 53 |
.get(
|
| 54 |
spreadsheetId=SAMPLE_SPREADSHEET_ID,
|
| 55 |
+
range=range_name
|
| 56 |
)
|
| 57 |
.execute()
|
| 58 |
)
|
| 59 |
values = result.get("values", [])
|
| 60 |
|
| 61 |
+
return values
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_team_numbers() -> list[str]:
|
| 65 |
+
values = get_range(f"Responses!I2:I{MAX_ROW}")
|
| 66 |
|
| 67 |
return [value[0] for value in values]
|
| 68 |
+
|
| 69 |
+
def get_questions_and_answers() -> list[list[str]]:
|
| 70 |
+
question_values = get_range("Responses!B1:F1")
|
| 71 |
+
|
| 72 |
+
questions = question_values[0]
|
| 73 |
+
|
| 74 |
+
answers_values = get_range(f"Responses!B2:F{MAX_ROW}")
|
| 75 |
+
|
| 76 |
+
questions_and_answers: list[list[str]] = []
|
| 77 |
+
for answers in answers_values:
|
| 78 |
+
for question, answer in zip(questions, answers):
|
| 79 |
+
questions_and_answers.append([question, answer])
|
| 80 |
+
|
| 81 |
+
session['questions_and_answers'] = questions_and_answers
|