Re: CSV Dowload

84 views
Skip to first unread message
Message has been deleted

Dave S

unread,
May 29, 2019, 6:54:40 AM5/29/19
to web2py-users


On Tuesday, May 28, 2019 at 11:00:42 PM UTC-7, Quang Lam wrote:
Hi, i have a problem to display the CSV file stored in the table.

the code below is in controller 

def view_performance():
    files = db.Pass_Configuration(request.args(0, cast=int)) or redirect(URL('index'))   // get the ID of Pass_Configuration table form the URL
    return dict(files=files)

def download():
    return response.download(request, db)           //download function 


the code in the View as follows

{{extend 'layout.html'}}
<h1>{{='Performance'}}</h1><br>
src="{{=URL('download', args=files.PerformanceFile)}}"   


This will indeed display the path.
 
i know i get the path to the file right but somehow it does not display the CSV file. it just displays the file path and nothing else. i know i am missing something but could not figure out
could you please help me to point out what the mistake i have and how to fix it. thanks

I know you will need to set the content type header (I do it in my controller function).  You may also need to stream the file, as I do for a picture

    filedata=open(dst,"rb").read()
    response
.headers['Content-Type']='image/jpeg'
    response
.headers['Content-Length']=imstat.st_size
   
return response.stream(cStringIO.StringIO(filedata))

Appadmin.py, though, only sets the content type and then returns a string (specifically the rows of the db query).

If you were returning a PDF, I think you're back to streaming.

/dps

 
Message has been deleted

黄祥

unread,
May 29, 2019, 3:01:19 PM5/29/19
to web2py-users

Quang Lam

unread,
May 29, 2019, 3:43:48 PM5/29/19
to web...@googlegroups.com
Hi Steve, the code mentioned in the book is only for export to CVS file. my situation is to download the CVS file stored in the table

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/a157283a-534f-44f9-8861-763768f0c6ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
May 29, 2019, 4:07:21 PM5/29/19
to web2py-users


On Wednesday, May 29, 2019 at 10:15:49 AM UTC-7, Quang Lam wrote:
Hi Dave

Could you please show me more detail in code since i need to download the CSV file store in the table?

Look at appadmin.py around line 5824.  The file is in yourapp/controllers.

Basically:

   get query
   
set headers # it does 'Content-disposition' as well as 'Content-Type'
   
return str(db(query, filters).select())

You may not want the 'Content-disposition' header -- it's setting attachment and filename.

If you're doing an existing csv file, rather than query results, then you need to read the file and either stringify it or stream it.

But CSV is an ugly thing to show to a user; if the user needs query results in a table, then either SQLTABLE or SQLFORM.grid could be used, and I think the latter could also be used for a file you read.

/dps



On Tuesday, May 28, 2019 at 11:00:42 PM UTC-7, Quang Lam wrote:
Hi, i have a problem to display the CSV file stored in the table.

the code below is in controller 

def view_performance():
    files = db.Pass_Configuration(request.args(0, cast=int)) or redirect(URL('index'))   // get the ID of Pass_Configuration table form the URL
    return dict(files=files)

def download():
    return response.download(request, db)           //download function 


the code in the View as follows

{{extend 'layout.html'}}
<h1>{{='Performance'}}</h1><br>
src="{{=URL('download', args=files.PerformanceFile)}}"   

Dave S

unread,
May 29, 2019, 4:23:01 PM5/29/19
to web2py-users


On Wednesday, May 29, 2019 at 1:07:21 PM UTC-7, Dave S wrote:
[...]
 
You may not want the 'Content-disposition' header -- it's setting attachment and filename.

[...]

But CSV is an ugly thing to show to a user; if the user needs query results in a table, then either SQLTABLE or SQLFORM.grid could be used, and I think the latter could also be used for a file you read.

Ah, I see elsewhere that you are using the grid, and are trying to give the user a download.  So, good!  And you do want the 'Content-disposition' header.

/dps

Quang Lam

unread,
May 29, 2019, 4:37:41 PM5/29/19
to web...@googlegroups.com
Hi Dave, yeah, i changed to grid since it has the download function, but when i click on it, it navigates me to the next page with the name of file in URL and 404 not found. do you know how to fix this problem?  you can see my code in detail in the other post named CVS file download.

Thanks 

--
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+un...@googlegroups.com.

黄祥

unread,
May 29, 2019, 6:26:47 PM5/29/19
to web2py-users
for download all tables at once
try:
   
import StringIO
except ImportError:
   
from io import StringIO

def download_csv():
     stream
= StringIO.StringIO()
     db
.export_to_csv_file(stream,
                                delimiter
= ',',
                                quotechar
= '"',
                                write_colnames
= True,
                                represent
= False)
     response
.headers['Content-Type'] = 'text/csv'
     response
.headers['Content-Disposition'] = 'inline; filename="{0}.csv"'.format(request.function.replace('_', ' ').title() )
     
return stream.getvalue()

modified the code before db.export_to_csv_file() to rows select to download one table at a time

best regards,
stifan
Message has been deleted

Kim Cuc

unread,
May 30, 2019, 8:38:08 AM5/30/19
to web...@googlegroups.com, mrry...@csu.fullerton.edu
Reply all
Reply to author
Forward
0 new messages