flask - 谷歌云函数 HTTP 触发器
问题描述
我试图通过添加 URL 参数来为我的函数提供参数。
import json
import flask
def location_sort(request):
request_json = request.get_json()
location = json.dumps(request_json)
location = json.loads(location)
reverse_location = {v: k for k, v in location.items()}
x = location.keys()
harf_x = (float(max(x)) + float(min(x))) / 2
y_right = []
y_left = []
sorted_location = []
for i in location:
if float(i) < harf_x:
y_left.append(location[i])
else:
y_right.append(location[i])
y_left.sort()
y_right.sort(reverse=True)
sorted_input = y_left + y_right
for i in sorted_input:
sorted_location.append([reverse_location[i], i])
for i in sorted_location:
i[0],i[1] = float(i[0]),float(i[1])
return sorted_location
def cal_centroid(location):
area = 0 # 면적
centroid_x = 0
centroid_y = 0
temp = 0
for i in range(len(location)):
if i == len(location)-1:
temp = location[i][0]*location[0][1] - location[0][0]*location[i][1]
area += temp*0.5
centroid_x += (location[i][0] + location[0][0]) * temp
centroid_y += (location[i][1] + location[0][1]) * temp
else:
temp = location[i][0]*location[i+1][1] - location[i+1][0]*location[i][1]
area += temp*0.5
centroid_x += (location[i][0] + location[i+1][0]) * temp
centroid_y += (location[i][1] + location[i+1][1]) * temp
centroid_x = round(centroid_x / (6*area), 6)
centroid_y = round(centroid_y / (6*area), 6)
x = [centroid_x, centroid_y]
return json.dumps(x)
def main(request):
request_args = request.args
if request_args and "location" in request_args:
request = request["location"]
request = json.dumps(request)
a = location_sort(request)
return cal_centroid(a)
这是我的 Cloud Function 代码,我运行 main 函数。我尝试了 URL 作为
https://<REGION>-<GOOGLE_CLOUD_PROJECT>.cloudfunctions.net/FUNCTION_NAME?location={"37.284213":"127.006481","37.562045":"127.034809","37.528694":"126.907483","37.411124":"127.124356"}
它返回
Error: could not handle the request
我的代码可能有什么问题?我是 GCF 的初学者,非常感谢您的帮助:)
解决方案
推荐阅读
- html - Middleman 博客分页显示所有文章
- python - 在 (.xlsx) 文档的末尾追加一行
- azure - 将 DevOps 项目迁移到新租户
- html - Bootstrap 折叠“in”类没有效果
- javascript - 为什么使用空字符串作为搜索参数发出获取请求会导致返回随机数据而不是没有数据?
- python - 如何使用列中的数据创建列,但取决于此数据的出现
- java - 如何从 selenium java 测试文件将参数传递给空手道功能文件
- swift - AV麦克风的基础和要求
- javascript - 将已知长度数组转换为单独变量的简便方法
- android - Altbeacon:从通知 AddAction 禁用前台模式和终止应用程序