Hi all,
can you recommend an elegant way to download CSV file from web2py?
Using gluon code, I wrote a controller function that creates good CSV
for me in cStringIO object, but my recordsets are large and I'd like
to stream download file. I.e. approach when I write temp file and then
download it is not good - I want to start file streaming to user
immediately, definitely this involves some manipulations with response
object...
My controller:
def download_csv():
def none_exception(value): #taken from gluon.sql
if isinstance(value, unicode):
return value.encode('utf8')
if hasattr(value, 'isoformat'):
return value.isoformat()[:19].replace('T', ' ')
if value == None:
return ''
return value
sqlset = ........
rs = db(sqlset).select()
outfile = cStringIO.StringIO()
writer = csv.writer(outfile, dialect='excel-tab')
columns = [f for f in fields_array if not f.adv_omit_download]
row = [T(f.name) for f in columns]
writer.writerow(row)
for rec in rs:
row = [none_exception(eval('rec.' + str(f))) for f in columns]
writer.writerow(row)
outfile.getvalue() #contains valid CSV string but I want to stream
it to user during cycle over rs
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---