#from queue import Queue#from flask import Response
queue = Queue()
defevent_stream():
whileTrue:
# Question: how to break out of this loop when the app is killed?
message = queue.get(True)
print("Sending {}".format(message))
yield"data: {}\n\n".format(message)
@app.route('/api/stream')
defstream():
return Response(event_stream(), mimetype="text/event-stream")
A YouTube video was the first time I encountered url_for feature. I couldn't understand what it was, for what it is. And today, I began to read the official manual
Let's say we need to a number of dynamic html page. The page need to be changed based on data from outside. Without template, we need to make a static HTML page in manual way. "template" gives an easy way to replace page's content in a very simple way. it feels like a mail merge feature in a word processor.
As we want to make a dynamic page, we need to use this feature "template"
(I am following this Flask tutorial)
This instruction shows how to deal with template, url_for in Flask.
How to use "template"?
import class
in order to use "template", firstly we need to import a class named "render_template" in python file.
fromflaskimportFlask,render_template
preparing a HTML file in "templates" folder
I prepared "hello.html" inside of templates folder.
templates/hello.html
<!DOCTYPE html><html><head><metacharset="utf-8"><title></title></head><body><h1>This is hello world</h1></body></html>