Thanks Peter - that was very helpful indeed. I didn't manage to get a
'download to CSV from checkboxes' function working, don't quite get why
as i managed to set up a  'download links from rows ' function in the
SQLFORM.grid.

Here is  the code for you just in case it is any help for you in the future

*##So in models I have my Db*

db.define_table('animal',
                Field('species'),
                Field('genus'),
                Field('family'))


*##in controllers - default i have the following*

links = [lambda row: A('Download data
set',_href=URL("default","download_animal_dataset",args=[row.id]))]

def animal_data():
    db.animal.id.readable = False
    animals = SQLFORM.grid(db.animal, links = links,deletable=False,
editable=False, details=False, selectable=False, create=False, csv=False,
buttons_placement = 'right')
    return locals()

def download_animal_dataset():
    animal_id = request.args(0,cast=int)
    animals = db(db.animal.id == request.vars[0]).select()
    return dict(animals=animals)


*## and then in the view i have default/download_animal_dataset.html*

{{
    import cStringIO
    stream = cStringIO.StringIO()
    animals.export_to_csv_file(stream)
    response.headers['Content-Type'] = 'application/vnd.ms-excel'
    response.headers['Content-Disposition'] = 'attachment; filename="%s"' %
your_file_name
    response.write(stream.getvalue(), escape=False)
    }}


cheers

matt





On Tue, Jul 4, 2017 at 7:04 PM, Peter <perr...@gmail.com> wrote:

>
>
> Based on previous code, the companies query/function would look something
> like...
>
>
>     def csv_companies():
>         # assumes there is a table called 'company'    with field 'name'
>         query = db.company.id > 0
>         companies = db(query).select(db.company.id,
>                                   db.company.name,
>                                   # ...
>                       # the list of 'company' fields you want in your query
>                       # you can join other tables and include fields from
> them here as well
>                                   # ...
>                      )
>
>         return dict(companies=companies)
>
>
>
>
>
> The view (csv_companies.html) would be something like...
>
>     {{
>     import cStringIO
>     stream = cStringIO.StringIO()
>     companies.export_to_csv_file(stream)
>     response.headers['Content-Type'] = 'application/vnd.ms-excel'
>     response.headers['Content-Disposition'] = 'attachment; filename="%s"'
> % your_file_name
>     response.write(stream.getvalue(), escape=False)
>     }}
>
>
> Note it doesn't have
>
>     {{extend 'layout.html'}}
>
> as it pushes the output to a user dialogue asking if they want to
> open/save it somewhere on their machine.
> (You may want to do something different like simply write it to a
> directory or create a db entry referencing it...)
>
>
>
> --
> 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/UiCqbO9nnDw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> web2py+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to