[英]POST request with Frozen Flask
Having viewed examples on the web, it seems like one can use forms with POST requests inside static websites built with vanilla HTML/CSS/JS.在查看了 web 上的示例后,似乎可以将 forms 与使用普通 HTML/CSS/JS 构建的 static 网站中的 POST 请求一起使用。 But I'm unable to do this with Flask.
但我无法用 Flask 做到这一点。 I'm using the Frozen-Flask library .
我正在使用Frozen-Flask 库。
My main server.py
script is below ( model
and encoder
are loaded correctly at top of file)我的主要
server.py
脚本如下( model
和encoder
在文件顶部正确加载)
app = Flask(__name__)
app.config['FREEZER_RELATIVE_URLS'] = True
@app.route('/')
def main():
return render_template("index.html")
@app.route('/predict', methods=["POST"])
def predict():
if request.method == "POST":
message = request.form['submission'] # gets submission
prediction = model.predict([message]) # feeds to model
classification = encoder.inverse_transform(prediction) # decodes prediction
return render_template('index.html', message=message, classification=classification)
if __name__ == "__main__":
app.run(debug=True)
As well as my freeze.py
script:以及我的
freeze.py
脚本:
from flask_frozen import Freezer
from server import app
freezer = Freezer(app)
if __name__ == '__main__':
freezer.freeze()
Finally, the relevant portion of my index.html
:最后,我的
index.html
的相关部分:
<form class="form-group" action="{{ url_for('predict') }}" method="POST">
<textarea class="form-control" name="submission" id="submission" rows="10"></textarea>
<button type="submit" class="btn btn-primary">Classify</button>
</form>
If I run python server.py
, aka regular Flask, the website works fine.如果我运行
python server.py
,也就是常规的 Flask,网站运行良好。 It loads up at http://localhost:5000/
and clicking the Classify button brings me to http://localhost:5000/predict/
, with the classification shown on the screen.它在
http://localhost:5000/
加载并单击分类按钮将我带到http://localhost:5000/predict/
,屏幕上显示分类。
When I run python freeze.py
, aka Frozen-Flask to generate a static website, I run into errors.当我运行
python freeze.py
(又名 Frozen-Flask)来生成 static 网站时,我遇到了错误。
$ python freeze.py
Traceback (most recent call last):
File "freeze.py", line 11, in <module>
freezer.freeze()
File "C:\Users\User\Documents\Projects\Spam Classifier\spam-classifier-hoohacks-starter\env\lib\site-packages\flask_frozen\__init__.py", line 199, in freeze
return set(page.url for page in self.freeze_yield())
File "C:\Users\User\Documents\Projects\Spam Classifier\spam-classifier-hoohacks-starter\env\lib\site-packages\flask_frozen\__init__.py", line 199, in <genexpr>
return set(page.url for page in self.freeze_yield())
File "C:\Users\User\Documents\Projects\Spam Classifier\spam-classifier-hoohacks-starter\env\lib\site-packages\flask_frozen\__init__.py", line 183, in freeze_yield
new_filename = self._build_one(url)
File "C:\Users\User\Documents\Projects\Spam Classifier\spam-classifier-hoohacks-starter\env\lib\site-packages\flask_frozen\__init__.py", line 322, in _build_one
% (response.status, url))
ValueError: Unexpected status '405 METHOD NOT ALLOWED' on URL /predict/
What I've done to investigate / try to resolve this: The 405 error indicates that an HTTP request other than POST is being called on predict.我为调查/尝试解决此问题所做的工作:405 错误表明在预测中调用了除 POST 之外的 HTTP 请求。 So for testing purposes I
changed methods=["POST"]
to methods=["GET"]
.因此,出于测试目的,我
changed methods=["POST"]
更改为methods=["GET"]
。 And then I made my predict page just render the normal main page.然后我让我的预测页面只呈现正常的主页。
@app.route('/predict/', methods=["GET"])
def predict():
return render_template("index.html")
Running the freeze script no longer throws an error.运行冻结脚本不再引发错误。 The website exists at
.../build/index.html
and clicking the Classify button takes me to .../build/predict/index.html
.该网站位于
.../build/index.html
并单击分类按钮将我带到.../build/predict/index.html
。 But the predict page is not useful, since its the same as the main page.但是预测页面没有用,因为它与主页相同。
I'm guessing Frozen-Flask needs to have GET access to pages to build static versions of them, but shouldn't I also be able to update those pages with POST-supplied information?我猜 Frozen-Flask 需要对页面进行 GET 访问才能构建它们的 static 版本,但我不应该也可以使用 POST 提供的信息更新这些页面吗? Or is not being able to do that part of the definition of "static"
或者无法做到“静态”定义的那部分
From what I can see from the code, Frozen-Flask uses the test_client's GET method to obtain the content for each page to be generated.从代码中我可以看到,Frozen-Flask 使用 test_client 的 GET 方法来获取每个要生成的页面的内容。 So you won't be able to use POST without hacking Frozen-Flask.
因此,如果不破解 Frozen-Flask,您将无法使用 POST。
On the other hand, GET requests can include URL parameters:另一方面,GET 请求可以包含 URL 参数:
/predict?message=123456
So you could obtain your parameters like this:所以你可以像这样获得你的参数:
request.args.get('message', '')
Now, keep in mind that Forozen-Flask does not know what to send as arguments.现在,请记住 Forozen-Flask 不知道要发送什么作为 arguments。 So what I would do is something like this:
所以我会做的是这样的:
@app.route('/predict', methods=["GET"])
def predict():
message = "some message" # hardcode a value
prediction = model.predict([message]) # feeds to model
classification = encoder.inverse_transform(prediction) # decodes prediction
return render_template('index.html', message=message, classification=classification)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.