BasicSR
BasicSR is a Basic Streaming Response class. This class provides the most basic way with which you can easily add streaming to your Flask application.
How to use?
The constructor of BasicSR class accepts Arbitary Argument List (args
) and Keyword Arguments (kwargs
)
Every argument passed to constructor of BasicSR
should be an instance of class Dom
.
kwargs
allowed in BasicSR's constructor are the ones that are allowed with Response object of your Flask. For more details please refer to API Documentation of Flask specific to your version.
For example: For version Flask v0.11 Response object documentation is here
So the BasicSR that is being used with Flask v0.11 are
status
headers
mimetype
content_type
direct_passthrough
BasicSR object contains a property response
which is to be returned by your main flask endpoint. For more clarification check the sample below
Sample
from flask import Flask
from flasksr import BasicSR, Dom
app = Flask(__name__)
def render_menu():
return """
<ul style="list-style-type: none; margin: 0; padding: 0;">
<li><a href="/">Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
"""
def render_body():
return """
<div style="margin-top: 50px;">Hello World!</div>
"""
def render_first():
return """
<html>
<head>
<title>FlaskSR Example</title>
</head>
<body>
"""
def render_last():
return """
</body>
</html>
"""
@app.route('/')
def hello():
return BasicSR(
Dom(render_first),
Dom(render_menu),
Dom(render_body),
Dom(render_last)
).response
if __name__ == '__main__':
app.run(host='0.0.0.0')