Adding 'represent' to a table breaks Grid

78 views
Skip to first unread message

Wouter Lintzen

unread,
Feb 7, 2025, 12:08:00 PM2/7/25
to py4web

From the documentation Basic grid example adding represent to the table breaks the Grid.

See example below:

db.define_table(
   'person',
   Field('superhero'),
   Field('name'),
   Field('job', represent=lambda r: r if r else 'NA')

 

error:
  File "…\py4web\utils\grid.py", line 490, in compute

    value = col.represent(value, row)

            ^^^^^^^^^^^^^^^^^^^^^^^^^

TypeError: <lambda>() takes 1 positional argument but 2 were given

 

Regards,

Wouter

Massimo

unread,
Feb 8, 2025, 9:05:13 PM2/8/25
to py4web
That was the web2py represent function which is not supported in py4web. It must take two arguments, the value and the full row, even if unused (use _ if unused):

   Field('job', represent=lambda value, _: value or 'NA')



Jorge Salvat

unread,
Jun 21, 2025, 6:46:39 AM6/21/25
to py4web
Hi, after adding underscore as the second argument in represent grid keeps breaking:

Field("fecha_inicio", "date", requires=IS_DATE(error_message='Entrar fecha'),
          represent=lambda x, _: DATE_REPRESENT(x)),

had to modify  Grid.py  

1109   value = col.represent(row)
1109   value = col.represent(row, 'NA')

but now  render()  breaks

  File "C:\wwwroot\py4web-env\apps\ainmo\templates\parent.html", line 32, in template
    [[=grid.render() ]]
  File "C:\wwwroot\py4web-env\venv\Lib\site-packages\py4web\utils\grid.py", line 1313, in render
    return self._make_table()
           ^^^^^^^^^^^^^^^^^^
  File "C:\wwwroot\py4web-env\venv\Lib\site-packages\py4web\utils\grid.py", line 1262, in _make_table
    table.append(self._make_table_body())
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\wwwroot\py4web-env\venv\Lib\site-packages\py4web\utils\grid.py", line 1109, in _make_table_body
    value = col.represent(row, 'NA')
            ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: alquileres.<locals>.<lambda>() takes 1 positional argument but 2 were given


Massimo

unread,
Jun 21, 2025, 8:27:40 PM6/21/25
to py4web
I cannot reproduce this. I need to see a bit more code.

The represent on line 1109 is not the field. represent but a Column.represent and it is should not need to be changed.

This works well for me
db.define_table('thing',
Field('name', represent=lambda a,b: a.upper()))

@action("grid")
@action.uses(auth, "generic.html")
def _():
grid = Grid(db.thing, columns=[db.thing.name])
return locals()
whether or not I specify Columns.

Can you perhaps modify this example so that you can reproduce the error?



Reply all
Reply to author
Forward
0 new messages