rendering rows as a generator and displaying via SQLTABLE

52 views
Skip to first unread message

Vlad

unread,
Dec 23, 2020, 4:32:51 PM12/23/20
to web2py-users
I'm getting the rows as a result of the select:
rows = db(query).select(*fields)
and want to display as 
table = SQLTABLE(rows, _class='table')
but before displaying need to reformat based on field representation, so rendering first: 
rendered_rows = rows.render()
now I simply want to display the rendered_rows, like this: 
table = SQLTABLE(rendered_rows, _class='table') 
which is impossible because rendered_rows is a generator. I can iterate row by row, but don't know how to use it with SQLTABLE. 

Is there an easy way to transform the generator into the format that SQLTABLE understands? 
 

Alex Beskopilny

unread,
Dec 24, 2020, 8:44:47 AM12/24/20
to web2py-users
Hi! 

You can create a table in memory and use grid
working example:

def pro_report1():
    tbl = 'proverka'

    #f_short_proverka = [  'f' + str(e) for e in short_proverka ]

    l = [e for e in db[tbl].fields if e in f_short_proverka ]
    memf = [ Field('oid', 'integer', label='П id', default= 0) ] + \
           [ Field(db[tbl][e].name, db[tbl][e].type, label = db[tbl][e].label,
             length= db[tbl][e].length, represent= db[tbl][e].represent  )
             for e in l ]

    dbmem = DAL('sqlite:memory')
    dbmem.define_table('mem_table',*memf)

    records = db(db[tbl].id>0).select().as_list()
    for r in records:
        r['oid'] = r['id']
        dbmem.mem_table.insert(**dbmem.mem_table._filter_fields(r))

    #dbmem.commit()

    flds= [ db[tbl][e] for e in f_short_proverka ]
    rows=db(db[tbl].id>0 ).select( *flds  )

    exportclasses=dict(
            csv_with_hidden_cols=False,
            csv=False,
            xml=False,
            json=False,
            tsv_with_hidden_cols=False,
            tsv= False,
            )

    dbmem.mem_table.id.readable = False
    grid = SQLFORM.grid(dbmem.mem_table, create=False, editable=False, exportclasses= exportclasses,
            deletable=False, buttons_placement = 'left', showbuttontext=False)
    return dict(rows=rows, l=l, records=records, grid=grid)
Reply all
Reply to author
Forward
0 new messages