There is an example of string streaming on the site, and I have
implemented my streaming controller function using this example (yes,
it involves diving into file interface but not too much):

#class streams long CSV to file-like object
#I pasted it here unchanged, so code may be excessive
class CSVstreamer():
    def __init__(self, conditions, output_fields):
        self.rs = db(eval(conditions)).select()
        self.outfile = cStringIO.StringIO()
        self.writer = csv.writer(self.outfile, dialect='excel-tab')
        self.columns = [f for f in output_fields if not
f.adv_omit_download]
        row = [T(f.name) for f in self.columns]
        self.writer.writerow(row)
        self.iterator = self.make_iterator()
    def make_iterator(self):
        for rec in self.rs:
            row = [none_exception(eval('rec.' + str(f))) for f in
self.columns]
            self.writer.writerow(row)
            yield self.outfile.getvalue()
            #the following is tricky part but without this file
redefining the thing does not work
            #it appears that if gluon streamer tries to stream
something that is not yet ready
            #it stops streaming until EOF is generated... better ask
Massimo why :-)
            self.outfile.close()
            self.outfile = cStringIO.StringIO()
            self.writer = csv.writer(self.outfile, dialect='excel-
tab')
    def read(self, n):
        n = self.iterator.next()
        return n

#controller function that streams LONG csv-string to browser
def download_csv():
    cond = 'db.table.id > 0'
    outfields = ['a', 'b', 'c']
    response.headers['Content-Type'] = 'application/octet-stream'
    response.headers['Content-Disposition'] = 'attachment;
filename=download.csv'
    return response.stream(CSVstreamer(cond, outfields), 64)

On May 26, 8:34 pm, hcvst <hcv...@googlemail.com> wrote:
> Hi,
>
> I would like to 'stream' results to the browser as they become
> available.
>
> Something like:
> ---
> response.write(lengthy_op(1))
> response.flush()
> response.write(lengthy_op(2))
> response.flush()
> ...
>
> I am now thinking of extending StringIO to write sth like
>
> response.stream(MyStringIO( [ (lengthy_op, 1), (lengthy_op, 2) ] ))
>
> but I don't feel like diving into the file interface and ending up
> with probably messy code
> if there is a solution available already.
>
> As always, thanks for web2py and the great support,
> HC
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to