Reputation: 6197
I am trying to test out redirects in flask. I can go directly to an endpoint url no problem, but when I try to redirect to it, I get an error message.
Edit: I changed the name of the method, call, and url to 'predict' but still getting the same error.
This is what my code looks like
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()
At this particular instance, its running on
http://688adffe.ngrok.io/QueryParser
When I put in a context and query and hit ask, nothing happens, and I see this in my commandline output
* 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 -
It works just fine if I go directly to the url
http://688adffe.ngrok.io/predict/test1/test2
I get this in the command line output
127.0.0.1 - - [21/Feb/2020 02:07:28] "GET /ModelInference/test1/test2 HTTP/1.1" 200 -
And there is an output (though I was aiming to have the output passed to the html but I can worry about that later)
{
"answer": "test1",
"context": "test2"
}
To recreate the minimal example, use this code which downloads files from my google drive. Note that flask versions above 1.0 sometimes do not work with flask_ngrok. In some cases, only version 0.12.5 or 0.12.4 works.
!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
For convenience, here's a google colab link that has this code ready to execute
https://colab.research.google.com/drive/1uxbR0-c75njIq5dckSpKVaklZkrTb4kf
Upvotes: 0
Views: 962
Reputation: 8245
The first argument to url_for
should be the name of the view function. It should be
return redirect(url_for('predict', question=question_textN, context=context_textN))
From the Docs
flask.url_for(endpoint, **values)
Generates a URL to the given endpoint with the method provided.
Also,
From your server output
127.0.0.1 - - [21/Feb/2020 03:52:01] "POST / HTTP/1.1" 404 -
It seems that you have not set the form action
attribute correctly to point to the query view function.
<form method="post" action="{{url_for('query')}}">
...
</form>
Upvotes: 1