Please help. What code would it take to make a simple text clock application with python and flask to demonstrate how flask streaming works? Ideally the app would show text time in place, overwriting on screen, from the once per second updating stream.
The clock stream is a simplified case of a real time text stream. The real underlying need is for a server to push out simultaneous video & text streams (no audio) like Miguel Grinberg's Video Streaming with Flask and to show both updates on screen on the client side. Miguel's video stream demo works. However I do not yet know how to get a simultaneous text stream to work.
I have tried the code below and there are some quirks:
- The html p element is just a place holder for text. It could be changed to anything that might work better.
- http://localhost:5000 shows '/time_feed' when I want it to instead show the time string contents of /time_feed.
http://localhost:5000/time_feed shows nothing at all while the flask server is running. When the flask server is stopped all of the sudden a bunch of updates appear in the browser like:
2018.11.01|20:29:272018.11.01|20:29:282018.11.01|20:29:292018.11.01|20:29:302018.11.01|20:29:312018.11.01|20:29:322018.11.01|20:29:33
I tried Streaming data with Python and Flask but do not understand how to apply either the javascript or jinga answers to my clock's code. I am new learning web and flask development.
app.py:
#!python3 Flask
# streaming clock per http://flask.pocoo.org/docs/1.0/patterns/streaming
from flask import Flask, Response, render_template, url_for
from datetime import datetime
import time
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/time_feed')
def time_feed():
def generate():
while True:
yield datetime.now().strftime("%Y.%m.%d|%H:%M:%S")
time.sleep(1)
return Response(generate(), mimetype='text')
if __name__ == '__main__':
app.run(debug=True, threaded=True)
./templates/index.html:
<title>Clock</title>
<h1>Clock</h1>
<p>{{ url_for('time_feed') }}</p>