FLASK-在同一呈现页面上显示API调用的结果



API调用的响应文本显示在日志中,但如何显示输出中的一个值。例如,下面将返回结果。

{"uuid":"FqDSDSVJY+GYAHSTSGA=","id":6238236373}

我想要呈现";6238236373";在我的index.html页面上。转换为有效的json对象,然后在我的页面上调用这些对象的最佳选项是什么?

app.py

@app.route('/alert', methods=['GET', 'POST'])
def alert():
form = ReusableForm(request.form)

if request.method == 'GET': 

import requests
url = "https://xxxxxxxxxxxxxxxxxxxxx.com/api/v1"
payload="{}"
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, application/xml',
'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

return render_template('index.html', form=form,)

index.html

<html>
<body>
{ render id response }
</body>
</html>

考虑到以json格式发送和接收数据,我认为可以使用jsonresult.text转换为字典。

import json
#...
data = json.loads(response.text)
#...
print(data['id']) # should output 6238236373

有关json的更多信息,请点击此处。

最新更新