使用 Flask 向本地服务器打印使用 [​​=10=] 请求接收的数据

Printing Data Received using POST Request to local server using Flask

所以,显然我正在尝试将数据从我的 openCV 网络摄像头发送到使用 Flask 旋转的本地服务器。我能够接收数据并将其打印在终端上,但是,我不太确定如何在网页上打印它。

这是我的程序:

from flask import jsonify, Flask, make_response,request, render_template
from flask_restful import Resource, Api

# creating the flask app 
from flask import jsonify, Flask, make_response,request, render_template
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)



@app.route("/getData", methods=['POST', 'GET'])
def get():
        if request.method == 'POST':
                textInput = request.form["data"]
                print(textInput)
                return render_template("text.html",text=textInput)
        else:
                return render_template("text.html")

@app.route("/", methods=['GET'])
def contact():
        return render_template("index.html")

if __name__ == '__main__':
    app.run(debug=True)

我正在通过 post 请求使用请求模块从 webcam.py 发送数据。数据已接收并当前打印在终端上。但是,我希望它被重定向到 text.html。

data = {"data": res} 
requests.post(url = API_ENDPOINT, data = data)

以上是我用来将数据从webcam.py发送到API_ENDPOINT (127.0.0.1:5000/getData)的代码片段。

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Sign to Speech</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        html,
        body {
            background-color: #FFC107
        }
    </style>
</head>

<body>

       <h4>{{text}}</h4>


</body>

</html>

以上是我在templates目录下的text.html页面。 任何帮助将不胜感激 :D

试试下面这个:

from flask import jsonify, Flask, make_response,request, render_template
from flask_restful import Resource, Api

# creating the flask app 
app = Flask(__name__) 
# creating an API object 
api = Api(app) 

@app.route("/getData", methods=['POST', 'GET'])
def getInfo():
        textInput = request.form["data"]
        print(textInput)
        return render_template("text.html",text=textInput)
if __name__ == '__main__':
    app.run(debug=True)

然后在您的 HTML 中使用 Jinja,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
{{ text }}
</body>
</html>

您的代码的问题是您将数据从 OpenCV webcam 发送到 local server,然后从 local server 您 return 响应 openCV webcam 并且这就是为什么您在打印时会在终端中看到数据,而在 Flask 应用程序的网页中看不到数据,因为您没有该数据,因为您在 returned 时丢失了数据对 openCV webcam.

的回应

在这种情况下,您可以使用 3 种方法中的一种。

  1. 使用数据库,例如 sqlite3 并保存从 openCV webcam 接收到的数据,但是您需要做更多的事情,例如创建模型等

  2. 将从 OpenCV webcam 收到的数据保存到文件中 - 验证一切正常的更快选项(我将在我的代码示例中使用的那个)

  3. 使用 flask.session 并将数据保存到 Flask 会话中,然后像从 python 字典中读取数据一样从中读取数据。

在这些情况下,当您在浏览器中打开 Flask Web 应用程序时,您需要从 DBfileflask.session 中读取数据。

在这个例子中,我将使用一个名为 data.txt 的文件来写入(我将使用 a 这意味着打开文件追加到文件的末尾,然后是旧的当您从 OpenCV webcam 发送多个请求时,数据将被保留)从 OpenCV webcam 服务器收到的信息。

from flask import Flask, request, render_template, jsonify

# creating the flask app
app = Flask(__name__)


@app.route("/getData", methods=['POST', 'GET'])
def getInfo():
    if request.method == 'POST':
        text_input = request.form["data"]
        with open('data.txt', 'a') as data_file:
            data_file.write(text_input)
        return jsonify({'message': 'Data saved sucessfully!'}), 200
    else:
        text_input = None
        with open('data.txt', 'r') as data_file:
            text_input = data_file.read()
        return render_template("text.html", text=text_input)


if __name__ == '__main__':
    app.run(debug=True)

这样您的 OpenCV webcam 将收到 200 回复消息。然后你可以导航到你的网络应用 /getData 页面,然后请求方法将是 GET 然后它将读取文件的内容 data.txt 并将其传递给你的网页刚刚打开。

Make sure that you can access data.txt, it should be placed in the same directory as your app exists (at least with this example, but you should make more appropriate structure later on or at all use the sqlite3 database for local development).