starred expression with tuple or array !? differente behaviour!

13 views
Skip to first unread message

António Ramos

unread,
Apr 18, 2022, 12:20:49 PM4/18/22
to py4web, web...@googlegroups.com
Hello i have my code to generate html base on a condition

one of the table columns should be created only if var =1 

this works but the html removes the _class="aa" in case of var==1, and does not create the TD if var!=1
TD(
...,
*(DIV("b",_class="aa") ) if var==1 else tuple())
....,
etc


Using a list
If i use a list it shows the _class="aa" in case of var==1 but how do i make it to ommit the column in case var!=1 ??
It creates a blank TD() and that is not what i want 
TD(*[DIV("b",_class="aa") if var==1 else None???])

regards
António


Jim S

unread,
Apr 18, 2022, 2:47:17 PM4/18/22
to web2py-users
Did you see my reply in the py4web group?

--------------------------

If I'm understanding the question correctly, I think I'd do the following:

cell_list = []
cell_list.append(TD("...first td here..."))
cell_list.append(TD("...additional cell..."))
cell_list.append(TD("...additional cell..."))

if var == 1:
   cell_list.append('my conditional cell')

cell_list.append(TD("...remaining cells..."))
cell_list.append(TD("...remaining cells..."))
cell_list.append(TD("...remaining cells..."))

Or, if you want to specify all the standard cells at one time...

cell_list = [TD(), TD(), TD(), TD(), TD())
if var == 1:
    cell_list.insert(3, TD('conditional cell'))


Does that help?

-Jim


Reply all
Reply to author
Forward
0 new messages