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

On Wednesday, May 13, 2015 at 9:24:59 AM UTC-4, Prasad Muley wrote:
>
> 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 <abasta...@gmail.com> 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 
>> web2py+unsubscr...@googlegroups.com.
>> 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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to