I have a method that returns float variables and I m streaming it to an HTML template using StreamingHttpResponse in Django, to catch the variable I found a way to use XMLHttpRequest in ajax as shown in the code below:
#method to return speed as a list of three float values
def gen_speed(video_feed):
while True:
speed, frame, gray, output = video_feed.get_frames()
yield speed
def speed_stream(request):
try:
pathVideo = r"static\app_resources\videos\rl4_pb8-7.mp4"
cam = model.video_feed(pathVideo)
stream = model.gen_speed(cam)
response = StreamingHttpResponse(stream,
status=200,
content_type='text/event-stream')
#response['Cache-Control'] = 'no-cache'
return response
except:
pass
this is code is in my HTML :
<script>
url = '{% url "test_stream" %}';
let xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", url, true);
xmlhttp.send();
function handleEvent(e) {
console.log(e.target.responseText);
}
xmlhttp.addEventListener('progress',handleEvent);
}
<script>
the results are like this :
as you see the streaming is working but the problem is that I don't know how to handle the values in the template, the values that XMLHttpRequest catches are not separated. what I want is a way to catch this dictionary and access to it so that I can give each value to a streaming chart each time.