Stream cStringIO

204 views
Skip to first unread message

flagist0

unread,
Dec 26, 2014, 3:14:33 PM12/26/14
to web...@googlegroups.com
Hello!
What is the current right way to stream data in web2py?
I'm trying to stream the data using cStringIO and response.stream, but as a best result I get an empty file.

Here is a source snippet:
rows = current.db(current.db.categories.belongs(categories)).select()
s = cStringIO.StringIO()
rows.export_to_csv_file(s, represent=False)
return response.stream(s, attachment=True, filename='categories.csv')

As a result I get an empty csv file, while s did contain the right data.
I couldn't find any clear documentation for response.stream, so I experimented with parameters, but it didn't work.
Do I have to set headers manually? Can I stream cStringIO instead of file object?

PS: http://web2py.com/examples/static/epydoc/web2py.gluon.globals.Response-class.html#stream and  http://web2py.com/examples/static/epydoc/web2py.gluon.globals-pysrc.html#Response.stream show different default keyword arguments:
stream(self, stream, chunk_size=65536, request=threading.local(), attachment=True, filename=threading.local())
and
def stream( self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None, attachment=False, filename=None)
correspondingly.

Niphlod

unread,
Dec 26, 2014, 5:27:02 PM12/26/14
to web...@googlegroups.com
the docstrings api on web2py.com/examples/epydoc are reeeally old and buggy. The new api documentation is on readthedocs

Now... if you want to pass a StringIO to response.stream, you have to "rewind" it to 0 before passing it ....

s = cStringIO.StringIO()
rows
.export_to_csv_file(s, represent=False)

s
.seek(0)   #<---

Alexandr Presniakov

unread,
Dec 29, 2014, 9:03:45 AM12/29/14
to web...@googlegroups.com
Thank you! Yes, it worked, but maybe it should be added into the response.stream code
 itself?

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/71tdm80FVls/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Dec 29, 2014, 10:03:26 AM12/29/14
to web...@googlegroups.com
nope. you could want to stream something at the middle of the stringio constructed stream. Dealing with seek() is basic python 101.
Reply all
Reply to author
Forward
0 new messages