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'
On Wed, May 13, 2015 at 6:08 PM, Anthony <[email protected]> wrote:
> 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
> <https://github.com/web2py/web2py/blob/master/gluon/sqlhtml.py#L3427>.
> You can tweak the "export" method to change the column names as you like.
>
> Anthony
>
>
> On Tuesday, May 12, 2015 at 1:07:40 PM UTC-4, Sujata Aghor wrote:
>>
>>
>> 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!!!
>>
> --
> 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
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
Thanks and Regards
Prasad M. Muley
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.