Export to csv which is not taking label from model file, where Grid takes.

119 views
Skip to first unread message

Sujata Aghor

unread,
May 12, 2015, 1:07:40 PM5/12/15
to web...@googlegroups.com

Hi,
In my model file I have defined table and Label for the fields like this -
db.define_table('mytable',
                Field('fname', 'string', length=12, writable=False,
                      notnull=True, label='First Name'),
               Field('lname', 'string', length=12, writable=False,
                      notnull=True, label='Last Name'),
...........................

1. In case of Grids, table headers are coming from  model file (shown above) No extra code has been written to show the labels from the model file.

2. So I was expecting the same headers would come to CSV file also, but surprisingly when I export to CSV we dont get these labels. (I tried by doing CSV=true in case of grid and still gets mytable.fname)

3. From documentation, I found Grid has additional parameter. i.e headers I tired to use that parameter to see if it works in export. So I defined headers as below :
headers = {'db.mytable.fname': 'First Name',
                   'db.mytable.lname': 'Last Name'
                  }

However export to CSV is still showing headers as db.mytable.fname

Kindly guide!
I suspect this is BUG in web2py export to csv which is not taking label from model file.

Thanks in Advance!!!

Niphlod

unread,
May 12, 2015, 3:53:33 PM5/12/15
to web...@googlegroups.com
the export are for raw processing, so they get the same result as you would get - for practical purposes - from db(query).select().as_csv()

Prasad Muley

unread,
May 13, 2015, 2:59:28 AM5/13/15
to web...@googlegroups.com
Can use colnames parameter of export_to_csv_file ?

I tried to use it But got following error.

<type 'exceptions.AttributeError'> 'Row' object has no attribute '_extra'


What is the format for colnames?

Anthony

unread,
May 13, 2015, 8:38:42 AM5/13/15
to web...@googlegroups.com, sujata...@onedelta.in
You can create your own CSV exporter:

SQLFORM.grid(...,
             exportclasses
=dict(csv=(MyExporterCSV, 'CSV',
                                     T
('Comma-separated export of visible columns.'))))

The MyExporterCSV class can inherit from gluon.sqlhtml.ExporterCSV. You can tweak the "export" method to change the column names as you like.

Anthony

Prasad Muley

unread,
May 13, 2015, 9:24:59 AM5/13/15
to web...@googlegroups.com, sujata...@onedelta.in
Hi Anthony,

       I've used the custom CSV exporter.  I tried to use colnames parameters of export_to_csv_file But got an error (mentioned earlier). Am I missing anything in following code??


class CSVExporter(object):
    """This class is used when grid's table contains reference key id.
       Exported CSV should contain reference key name of reference
       key not ids"""
    file_ext = "csv"
    content_type = "text/csv"

    def __init__(self, rows):
        self.rows = rows

    def export(self):
        if self.rows:
            s = StringIO()
            colnames = []
            for colname in self.rows.colnames:
                    colnames.append(colname.split('.')[-1].replace('_', ' ').upper())
            print "DEBUG: colnames", colnames
            self.rows.export_to_csv_file(s, represent=True,
                                                         colnames=colnames)
            return s.getvalue()
        else:
            return ''


It successfully prints column name
DEBUG: ['FNAME', 'LNAME']

I got following error 

127.0.0.1.2015-05-13.18-52-30.6eb87877-1bb1-49e0-9f05-8508333fa967

<type 'exceptions.AttributeError'> 'Row' object has no attribute '_extra'

--
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/tJafWvYxcdw/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.



--
Thanks and Regards
Prasad M. Muley

Anthony

unread,
May 13, 2015, 8:09:57 PM5/13/15
to web...@googlegroups.com, pmm...@gmail.com, sujata...@onedelta.in
Yeah, I'm not sure you can use Rows.export_to_csv_file (at least, I don't think the colnames argument will help, as that simply specifies which columns you want to include -- it's purpose is not to rename columns). I suppose you could instead suppress the column names altogether, and then prepend your own column names to the returned object. Or just create your own custom CSV writer.

Anthony
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages