Issue
I've been making Flask api that gets table's name parameter and passes SQL to BigQuery with this parameter and return the list that contains all column names of table's name client sent. I confirmed this function does work as my ideal in Python but If I try to transform this function into Flask , it doesn't work well with error like "TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement". Actually , I've already made some api with Flask but this time I have no idea how to do at all . Is there any syntax error ?? Could you give me any advices ??
Flask
import urllib.request
import urllib.parse
import json
from google.cloud import storage
from google.cloud import bigquery
from flask import Flask, request, jsonify, Response
app = Flask(__name__)
@app.route('/')
def get_tags():
tableName = request.args.get('tableName') or ''
client = bigquery.Client()
query = """SELECT column_name
FROM `testProject.testTable.INFORMATION_SCHEMA.COLUMNS`
where table_name = '{}' """.format(tableName)
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("tableName", "STRING", tableName)
]
)
query_job = client.query(query)
query_res = query_job.result()
tag_list = []
for row in query_res:
tag_list.append(row[0])
response = Response(json.dumps(tag_list))
response.headers['Access-Control-Allow-Origin'] = '*'
return print(response)
if __name__ == "__main__":
app.run(debug=True)
Python
import json
from google.cloud import bigquery
import requests
def get_tags(self):
client = bigquery.Client()
query = """SELECT column_name
FROM `testProject.testTable.INFORMATION_SCHEMA.COLUMNS`
where table_name = '{}' """.format(tableName)
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("tableName", "STRING", tableName)
]
)
query_job = client.query(query)
query_res = query_job.result()
tag_list = []
for row in query_res:
tag_list.append(row[0])
return print(tag_list)
tableName = "test"
get_tags(tableName)
The result
['pgid', 'last_login_date', 'start_chat_numbers', 'username', 'luid', 'Q1_start', 'Q1_start_date', 'Q2_start', 'Q2_start_date', 'Q3_start', 'Q3_start_date', 'Q4_start', 'Q4_start_date', 'Q5_start', 'Q5_start_date', 'first_purchased', 'first_purchased_end', 'first_purchased_date', 'first_purchased_end_date', 'first_purchased_start', 'first_purchased_start_date', 'guidance1', 'guidance1_date', 'guidance2', 'guidance2_date', 'main_end', 'main_end_date', 'main_start', 'main_start_date', 'push1', 'push1_date', 'push1_end', 'push1_end_date', 'push1_start', 'push1_start_date', 'last_unfollow_date', 'last_follow_date', 'cv', 'orderid']
Solution
I checked GAE log and found out "from google.cloud import storage" caused error and I removed this and it works fine . Thank you so much !
Answered By - amaturePy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.