Как я могу передать переменную из flask) o HTML без шаблона визуализации? - PullRequest
0 голосов
/ 17 февраля 2020

Я новичок в использовании Flask, и я просто пытался передать переменную на веб-страницы. Я знаю, как передать переменную в методе render_template

, но сейчас я пытаюсь использовать потоковое приложение в реальном времени. Он использует различные методы, такие как .responses и возвращает

код flask для потоковой веб-камеры, приведенный ниже

@app.route('/')
def index():
    return render_template('index.html')

def get_frame():
    video=cv2.VideoCapture(0) #this makes a web cam object

while True:
    ret, frame = video.read()
    imgencode=cv2.imencode('.jpg',im)[1]
    stringData=imgencode.tostring()
    yield (b'--frame\r\n'
        b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')

del(video)

@app.route('/calc')
def calc():
return Response(get_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
app.run(host='localhost', debug=True, threaded=True)

, а шаблон

 <html>
 <head>
 <title>Video Streaming Demonstration</title>
 </head>
 <body>
 <h1>Video Streaming Demonstration</h1>
 <img src="{{ url_for('calc') }}">
 </body>
 </html>

Теперь мне нужно добавить метку к моему шаблону, например <label>{{ value }}</label>>

Я вычислил значение переменной из flask, и я могу напечатать его на консоли, но теперь мне нужно передать это значение метке на том же шаблоне, который потоковое видео. пожалуйста помогите

1 Ответ

0 голосов
/ 17 февраля 2020

FLASK APP.py

#This route accepts request against the url http://domain_name/change_label
#You could add the argument, method=["POST"],<br>So the route only accepts post request against that url.
@app.route("/change_label")
def change_label():
    #Return the text you want the label to be
    return "New Label"

JAVASCRIPT_FILE

function change_label(){
//Create xhttp object
var xhttp = new XMLHttpRequest();
//Listen for the response of your xhttp object
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    //If the request is 200 aka succesfull, set the text of the label to the response text of the xhttp request
    document.getElementById("Id of the label").innerHTML =
    this.responseText;
  }
};
//Open a post request with the xhttp object. 
xhttp.open("POST", "http://domain/change_label", true);
// Send the post request
xhttp.send();
}

HTML FILE

 <html>
 <head>
 <title>Video Streaming Demonstration</title>
 <script src="link_to_javascript"></script>
 </head>
 <body>
 <h1>Video Streaming Demonstration</h1>
 <img src="{{ url_for('calc') }}">
 </body>
 </html>

Извините, если это трудно понять, я очень плохо объясняю вещи, мой мозг просто не подключен нормально

...