On Apr 10, 12:40 pm, Johann Spies <[email protected]> wrote:
> I do not understand the code in the manual and what I have seen on
> this list about csv-downloads.
>
> I don't have a problem to do csv-export from the commandline in a
> shell. I just cannot figure out how the examples I referred to work
> in a MVC -environment.
>
> Take for example this export function (taken from a mail in this list):
>
> def export():
> import cStringIO
> s = cStringIO.StringIO()
> db.export_to_csv_file(s)
> response.headers['Content-Type'] = 'text/csv'
> return s.getvalue()
>
> My problem is that I do not understand what it does - especially the
> "s=cStringIO.String(IO()" part.
Think of this as file-io, but to a buffer, more specifically a string
buffer.
s=.... sets up the "file" (er, StringIO instance, which manages the
buffer, and provides file-io like interface);
export_to_csv_file takes a file argument; for this purpose, this is
a little contorted - it's basically saying something like this:
I want to convert to a csv string, then write that csv to a file...
but I'm a web-app; I don't _really_ want to write a file; I want to
write an http-response to a client. Oh, heck:
- I'll use StringIO to "fake" a file... (and write to a string
buffer)
- I'll prepare to send a response with the "file type" (text/csv), as
if the client were downloading a file...
- then I'll just get the text / the string out of that "in memory,
fake file" ==> s.getvalue() basically gets the "content" out of that
"s" IO buffer.
No wonder you're confused!!!!
I like this much better, as it is direct:
I'm sending something to a client, via an http-response.
Only, I am not going to send html. Heck, I'll just let the object's
represent function output it; Turns out that tables are (by default)
represented this way, so I can do this:
def something_as_csv():
# go ahead and make my selection, whatever it is
# myfields might be "ALL", or might be result of a join - really
can be rich!
rows = db(query).select(myfields)
response.headers['Content-Type']='text/csv'
return str(rows)
So, now - instead of understanding what StringIO does, you'll have to
go lookup the class of rows, and what it's __str__ functions are
doing.
But at least this is more direct, and closer to real intent: get the
data, tell the client we're sending text, and in fact csv... and send
client the string representation of the data (no too much "magic").
Hope this has been useful.
Regards,
- Yarko
>
> Can somebody explain to me where does 's' get it's content from?
>
> I know on the commandline I can do:
> records = db(query).select(db.table())
> and then records.export_to_csv_file(open('somefile','w')).
>
> Say I have a set of records (result of a query) which the user can see
> in a view and the user wants to download that selection of records.
> The link refer to this 'export' controller. How do I do it?
>
> I would like to see an example of how to set the link up in the view
> an how the above controller is called in such a way that the user can
> download the file or open it in some program able to read csv-files.
>
> Regards
> Johann
> --
> "Finally, brethren, whatsoever things are true, whatsoever things are
> honest, whatsoever things are just, whatsoever things are pure,
> whatsoever things are lovely, whatsoever things are of good report;
> if there be any virtue, and if there be any praise, think on these
> things." Philippians 4:8
--
To unsubscribe, reply using "remove me" as the subject.