SQLFORM.grid Export and represent

272 views
Skip to first unread message

John Fraser

unread,
Apr 16, 2014, 12:58:12 PM4/16/14
to web...@googlegroups.com
Is there an easy way to strip HTML markup from SQLFORM.grid's CSV/TSV export?  I realize I probably need to create a custom export function, but curious if there is an argument or a one liner that would do the trick.  Or is there a better way to format the display?

I have the following controller function:

@auth.requires_login()
def index():
    response.title = 'Computer Inventory'
    db.inventory.id.readable = False
    db.inventory.cpu_status.represent = lambda cpu_status, row: \
        SPAN(row.cpu_status,
             _style='text-transform: uppercase; font-weight: bold; padding:5px; color: %s; background-color: %s' %
             ('black' if row.cpu_status == 'Staged' else 'white','red' if row.cpu_status == 'Repair' else 'gold'
             if row.cpu_status == 'Staged' else 'green' if row.cpu_status == 'In Use' else 'black'))
    grid = SQLFORM.grid(db.inventory, csv=True)
    return locals()


When I export as CSV/TSV (as displayed to the user) all values for db.inventory.cpu_status are wrapped in SPAN tags for example:

<span style='text-transform: uppercase; font-weight: bold; padding:5px; color: white; background-color: black'>VALUE</span>


Cliff Kachinske

unread,
Apr 16, 2014, 1:29:30 PM4/16/14
to web...@googlegroups.com
There must be something in the request that indicates you want csv or tsv.

If it's in the request.args, for example, something like this would work:

if 'csv' not in request.args and 'tsv' not in request.args:
    db.inventory.cpu_status.represent = lambda cpu_status, row: SPAN(
       ...

Anthony

unread,
Apr 16, 2014, 1:35:41 PM4/16/14
to web...@googlegroups.com
Also, I believe CSV does not use the represent attribute (i.e., it returns the raw values) -- only TSV converts the values via represent. So, maybe just disable TSV.

Anthony

John Fraser

unread,
Apr 16, 2014, 2:10:34 PM4/16/14
to web...@googlegroups.com
The problem is I need the values 'as displayed to the user'  for all referenced fields. So I  need the represent value for some fields and do not for others.

I was over thinking the problem and Cliff pointed me in the right direction....Thanks as this appears to work.  Vars not Args

if '_export_type' not in request.vars:

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

Reply all
Reply to author
Forward
0 new messages