librelist archives

« back to archive

Using yield

Using yield

From:
Thomas Beugin
Date:
2010-11-03 @ 14:25
Hello,

I' am a new user of flask :)

I have a application built with cherrypy :)

flash have the stream response? cherrypy is the function yield?


http://pastie.org/1269481


Best regards :)

Cordialement,
Beugin Thomas

Re: [flask] Using yield

From:
Armin Ronacher
Date:
2010-11-04 @ 00:09
Hi,

On 2010-11-03 3:25 PM, Thomas Beugin wrote:
> flash have the stream response? cherrypy is the function yield?
yield is a keyword in Python that creates generators which are often the 
base of streaming functionality in frameworks.  The underlaying Werkzeug 
system does support generators but Flask does not directly accept them. 
  You can however use them with a trick:

     from flask import Response

     @app.route('/foo')
     def foo():
         def generate():
             yield 'first part'
             yield 'second part'
         return Response(generate())

Regards,
Armin

Re: [flask] Using yield

From:
Armin Ronacher
Date:
2010-11-04 @ 00:10
Hi,

I have to correct myself.  This version should work:

     from flask import Response

     @app.route('/foo')
     def foo():
         def generate():
             yield 'first part'
             yield 'second part'
         return Response(generate(), direct_passthrough=True)


Regards,
Armin

Re: [flask] Using yield

From:
Thomas Beugin
Date:
2010-11-04 @ 04:12
Thanks armin :)

I will try :)

Cordialement,
Beugin Thomas



2010/11/4 Armin Ronacher <armin.ronacher@active-4.com>:
> Hi,
>
> I have to correct myself.  This version should work:
>
>     from flask import Response
>
>     @app.route('/foo')
>     def foo():
>         def generate():
>             yield 'first part'
>             yield 'second part'
>         return Response(generate(), direct_passthrough=True)
>
>
> Regards,
> Armin
>

Re: [flask] Using yield

From:
Thomas Beugin
Date:
2010-11-04 @ 21:40
Great its works

from flask import Flask, Response
import os
from werkzeug import Headers

app = Flask(__name__)

@app.route('/')
def hello_word():
	def download():
		fich = open("fring.avi","r+b")
		while True:
			data = fich.read(4096)
			if not data: break
			yield data
	header = Headers()
	header.add("Content-Type", "application/x-download")
	header.add('Content-Length',  str(os.path.getsize("test.zip")))
	header.add("Content-Disposition", "attachment; filename=test.zip")
	return Response(download(), headers=header, direct_passthrough=True)
	
if __name__ == "__main__":
	app.debug = True
	app.run()

Cordialement,
Beugin Thomas



2010/11/4 Thomas Beugin <kimkof@gmail.com>:
> Thanks armin :)
>
> I will try :)
>
> Cordialement,
> Beugin Thomas
>
>
>
> 2010/11/4 Armin Ronacher <armin.ronacher@active-4.com>:
>> Hi,
>>
>> I have to correct myself.  This version should work:
>>
>>     from flask import Response
>>
>>     @app.route('/foo')
>>     def foo():
>>         def generate():
>>             yield 'first part'
>>             yield 'second part'
>>         return Response(generate(), direct_passthrough=True)
>>
>>
>> Regards,
>> Armin
>>
>

Re: [flask] Using yield

From:
Heungsub Lee
Date:
2010-11-05 @ 01:38
Hi,

I think that flask.send_static_file() is more useful than yield output
buffer to serve a static file.

2010/11/5, Thomas Beugin <kimkof@gmail.com>:
> Great its works
>
> from flask import Flask, Response
> import os
> from werkzeug import Headers
>
> app = Flask(__name__)
>
> @app.route('/')
> def hello_word():
> 	def download():
> 		fich = open("fring.avi","r+b")
> 		while True:
> 			data = fich.read(4096)
> 			if not data: break
> 			yield data
> 	header = Headers()
> 	header.add("Content-Type", "application/x-download")
> 	header.add('Content-Length',  str(os.path.getsize("test.zip")))
> 	header.add("Content-Disposition", "attachment; filename=test.zip")
> 	return Response(download(), headers=header, direct_passthrough=True)
> 	
> if __name__ == "__main__":
> 	app.debug = True
> 	app.run()
>
> Cordialement,
> Beugin Thomas
>
>
>
> 2010/11/4 Thomas Beugin <kimkof@gmail.com>:
>> Thanks armin :)
>>
>> I will try :)
>>
>> Cordialement,
>> Beugin Thomas
>>
>>
>>
>> 2010/11/4 Armin Ronacher <armin.ronacher@active-4.com>:
>>> Hi,
>>>
>>> I have to correct myself.  This version should work:
>>>
>>>     from flask import Response
>>>
>>>     @app.route('/foo')
>>>     def foo():
>>>         def generate():
>>>             yield 'first part'
>>>             yield 'second part'
>>>         return Response(generate(), direct_passthrough=True)
>>>
>>>
>>> Regards,
>>> Armin
>>>
>>
>

Re: [flask] Using yield

From:
Thomas Beugin
Date:
2010-11-05 @ 04:16
Yep i know.

My prog relaying the content of another server.

I read the buffer of file with urllib et i send it to the client via yield


Cordialement,
Beugin Thomas



2010/11/5 Heungsub Lee <heung@sublee.kr>:
> Hi,
>
> I think that flask.send_static_file() is more useful than yield output
> buffer to serve a static file.
>
> 2010/11/5, Thomas Beugin <kimkof@gmail.com>:
>> Great its works
>>
>> from flask import Flask, Response
>> import os
>> from werkzeug import Headers
>>
>> app = Flask(__name__)
>>
>> @app.route('/')
>> def hello_word():
>>       def download():
>>               fich = open("fring.avi","r+b")
>>               while True:
>>                       data = fich.read(4096)
>>                       if not data: break
>>                       yield data
>>       header = Headers()
>>       header.add("Content-Type", "application/x-download")
>>       header.add('Content-Length',  str(os.path.getsize("test.zip")))
>>       header.add("Content-Disposition", "attachment; filename=test.zip")
>>       return Response(download(), headers=header, direct_passthrough=True)
>>
>> if __name__ == "__main__":
>>       app.debug = True
>>       app.run()
>>
>> Cordialement,
>> Beugin Thomas
>>
>>
>>
>> 2010/11/4 Thomas Beugin <kimkof@gmail.com>:
>>> Thanks armin :)
>>>
>>> I will try :)
>>>
>>> Cordialement,
>>> Beugin Thomas
>>>
>>>
>>>
>>> 2010/11/4 Armin Ronacher <armin.ronacher@active-4.com>:
>>>> Hi,
>>>>
>>>> I have to correct myself.  This version should work:
>>>>
>>>>     from flask import Response
>>>>
>>>>     @app.route('/foo')
>>>>     def foo():
>>>>         def generate():
>>>>             yield 'first part'
>>>>             yield 'second part'
>>>>         return Response(generate(), direct_passthrough=True)
>>>>
>>>>
>>>> Regards,
>>>> Armin
>>>>
>>>
>>
>

Re: [flask] Using yield

From:
Heungsub Lee
Date:
2010-11-05 @ 04:50
Aha, I got it.
Thanks for the reply.

2010/11/5 Thomas Beugin <kimkof@gmail.com>

> Yep i know.
>
> My prog relaying the content of another server.
>
> I read the buffer of file with urllib et i send it to the client via yield
>
>
> Cordialement,
> Beugin Thomas
>
>
>
> 2010/11/5 Heungsub Lee <heung@sublee.kr>:
> > Hi,
> >
> > I think that flask.send_static_file() is more useful than yield output
> > buffer to serve a static file.
> >
> > 2010/11/5, Thomas Beugin <kimkof@gmail.com>:
> >> Great its works
> >>
> >> from flask import Flask, Response
> >> import os
> >> from werkzeug import Headers
> >>
> >> app = Flask(__name__)
> >>
> >> @app.route('/')
> >> def hello_word():
> >>       def download():
> >>               fich = open("fring.avi","r+b")
> >>               while True:
> >>                       data = fich.read(4096)
> >>                       if not data: break
> >>                       yield data
> >>       header = Headers()
> >>       header.add("Content-Type", "application/x-download")
> >>       header.add('Content-Length',  str(os.path.getsize("test.zip")))
> >>       header.add("Content-Disposition", "attachment; filename=test.zip")
> >>       return Response(download(), headers=header,
> direct_passthrough=True)
> >>
> >> if __name__ == "__main__":
> >>       app.debug = True
> >>       app.run()
> >>
> >> Cordialement,
> >> Beugin Thomas
> >>
> >>
> >>
> >> 2010/11/4 Thomas Beugin <kimkof@gmail.com>:
> >>> Thanks armin :)
> >>>
> >>> I will try :)
> >>>
> >>> Cordialement,
> >>> Beugin Thomas
> >>>
> >>>
> >>>
> >>> 2010/11/4 Armin Ronacher <armin.ronacher@active-4.com>:
> >>>> Hi,
> >>>>
> >>>> I have to correct myself.  This version should work:
> >>>>
> >>>>     from flask import Response
> >>>>
> >>>>     @app.route('/foo')
> >>>>     def foo():
> >>>>         def generate():
> >>>>             yield 'first part'
> >>>>             yield 'second part'
> >>>>         return Response(generate(), direct_passthrough=True)
> >>>>
> >>>>
> >>>> Regards,
> >>>> Armin
> >>>>
> >>>
> >>
> >
>



-- 
*Heungsub Lee
Mobile: +82-10-3215-2380
Site: http://sublee.kr/
GitHub: http://github.com/sublee
*