I want to send a variable value to python from flask html/js through url_for().
Python Code :
@app.route('/video_feed/<device>')
def video_feed(device):
# return the response generated along with the specific media
# type (mime type)
print(device)
try:
return Response(gen(int(device)),mimetype = "multipart/x-mixed-replace; boundary=frame")
except Exception as e:
return Response(gen(device),mimetype = "multipart/x-mixed-replace; boundary=frame")
Desired in flask html file:
data.value = 0;
image.src = "{{ url_for('video_feed', device=data.value)}}";
But it does not work, I get the error:
jinja2.exceptions.UndefinedError: 'data' is undefined
This does work, however I can't use the variable data.value in url_for() :
image.src = "{{ url_for('video_feed', device=0)}}";
I have tried several different things like:
image.src = "{{ url_for('video_feed', device=${data.value})}}";
image.src = "{{ url_for('video_feed', device=$data.value)}}";
image.src = "{{ url_for('video_feed', device=%s)}}", data.value;
But nothing seems to work. My javascript is a bit rusty.
Any help would be appreciated!
Cheers!
url_for
is expanded on the server side, before the page gets sent to the browser and well before JavaScript runs.
If you know the number (for device
/data.value
) at page generation time, pass it in to the template via render_template()
.
If, however, you don't know the value until after the page is rendered, you're going to need to construct the img
element from JavaScript.