fun clock streaming text with python and flask fun clock streaming text with python and flask flask flask

fun clock streaming text with python and flask


in view you can avoid while and sleep in that example:

@app.route('/time_feed')def time_feed():    def generate():        yield datetime.now().strftime("%Y.%m.%d|%H:%M:%S")  # return also will work    return Response(generate(), mimetype='text') 

in template:

<p id="clock">Here will be date|time</p><script>    var clock = document.getElementById("clock");    setInterval(() => {        fetch("{{ url_for('time_feed') }}")        .then(response => {                response.text().then(t => {clock.innerHTML = t})            });        }, 1000);  </script>

in your example with video streem it is just trick, it is not solution for real video streaming, just because not provide audio. If you need for real video stream you need to use webRTC with Kurento-media-server for example.For python look on aiortc lib.


You totally misunderstand how it works. streaming doesn't mean it can transport data intermittently, but means it can transport huge data while don't need to load it all at the beginning.

So while using stream, you are still using just one request. This request is a whole one, your browser can only deal with the response after the whole data is transported. In your case, as you are using an infinite loop, your browser will just waiting for the data forever.

To achieve what you want, you have two options:

  1. Use js to keep sending request per second.
  2. Use websocket.

update

It turns out that some elements can work with special mimetype multipart/x-mixed-replace to achieve long polling. As I am not a front-end guy, maybe someone can supply or correct my answer.