Skip to content

Commit

Permalink
Merge branch 'streaming-responses' of github.com:smotornyuk/ckan into…
Browse files Browse the repository at this point in the history
… streaming-responses
  • Loading branch information
smotornyuk committed Jul 10, 2017
2 parents 81600ab + c7918cf commit 08b5b70
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ckanext/example_flask_streaming/plugin.py
Expand Up @@ -10,23 +10,25 @@


def stream_string():
u'''A simple view function'''
u'''Stream may consist of any common content, like words'''
def generate():
for w in u'Hello World, this is served from an extension'.split():
yield w
return streaming_response(generate())


def stream_template(**kwargs):
u'''A simple replacement for the pylons About page.'''
u'''You can stream big templates as well.'''
tpl = flask.current_app.jinja_env.get_template(u'stream.html')
gen = tpl.stream(kwargs)
# pass integer into `enable_buffering` to control, how many
# tokens will consist in every response chunk.
gen.enable_buffering()
return streaming_response(gen)


def stream_file():
u'''A simple replacement for the flash Hello view function.'''
u'''File stream. Just do not close it until response finished'''
f_path = path.join(
path.dirname(path.abspath(__file__)), u'tests/10lines.txt')

Expand All @@ -39,7 +41,7 @@ def gen():


def stream_context():
u'''A simple replacement for the flash Hello view function.'''
u'''Additional argument keep request context from destroying'''
html = u'''{{ request.args.var }}'''

def gen():
Expand All @@ -49,12 +51,14 @@ def gen():


def stream_without_context():
u'''A simple replacement for the flash Hello view function.'''
u'''You'll definitely get error attempting to get request info.'''
html = u'''{{ request.args.var }}'''

def gen():
yield flask.render_template_string(html)

# `with_context` set to False by default. Thus, you cannot use
# request context in this case.
return streaming_response(gen())


Expand Down

0 comments on commit 08b5b70

Please sign in to comment.