【发布时间】:2020-02-21 02:09:32
【问题描述】:
我正在尝试在烧瓶中测试重定向。我可以直接转到端点 url 没问题,但是当我尝试重定向到它时,我收到一条错误消息。
编辑:我将方法、调用和 url 的名称更改为“预测”,但仍然出现相同的错误。
这就是我的代码的样子
from flask import Flask, request, render_template, jsonify
from flask_ngrok import run_with_ngrok
from flask import redirect, url_for
app = Flask(__name__)
run_with_ngrok(app)
@app.route('/QueryParser', methods=['GET', 'POST'])
def query():
if request.method == 'GET':
return render_template('index.html', value='hi')
else:
body = request.get_json()
question_textN = body['question']
context_textN = body['context']
return redirect(url_for('predict', question=question_textN,
context=context_textN))
@app.route("/predict/<question>/<context>", methods=["GET"])
def predict(question, context):
question_text = question
context_text = context
return jsonify(answer=question_text, context=context_text)
if __name__=="__main__":
app.run()
在这个特定的实例上,它正在运行
http://688adffe.ngrok.io/QueryParser
当我输入上下文并查询并点击询问时,没有任何反应,我在命令行输出中看到了这一点
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Running on http://688adffe.ngrok.io
* Traffic stats available on http://127.0.0.1:4040
127.0.0.1 - - [21/Feb/2020 03:51:48] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [21/Feb/2020 03:51:48] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [21/Feb/2020 03:51:55] "GET /QueryParser HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2020 03:52:01] "POST / HTTP/1.1" 404 -
如果我直接访问 url 就可以了
http://688adffe.ngrok.io/predict/test1/test2
我在命令行输出中得到了这个
127.0.0.1 - - [21/Feb/2020 02:07:28] "GET /ModelInference/test1/test2 HTTP/1.1" 200 -
还有一个输出(虽然我的目标是将输出传递给 html,但我稍后会担心)
{
"answer": "test1",
"context": "test2"
}
要重新创建最小示例,请使用从我的谷歌驱动器下载文件的代码。请注意,1.0 以上的烧瓶版本有时不适用于 flask_ngrok。在某些情况下,只有版本 0.12.5 或 0.12.4 有效。
!pip install flask==1.0
!pip install flask-ngrok
import os
if not os.path.exists('templates'):
os.mkdir('templates')
%cd templates
!gdown --id 1-l3SlwyyNjSV-bzUnyw1ZpPaPQz3KUYP
%cd ..
!gdown --id 1s_lGCf_T0619RWZKBjQ_oES0jwmNSn2F
!python rest-testMin.py
为方便起见,这里有一个 google colab 链接,该链接已准备好执行此代码
https://colab.research.google.com/drive/1uxbR0-c75njIq5dckSpKVaklZkrTb4kf
【问题讨论】:
标签: flask