简体   繁体   中英

Trouble making a table with HTML.py in python

I'm trying to make a table showing different values in each cell, and depending on the value of cell, have a different bgcolor .

So I have managed to do this successfully with one column by using a simple loop, but I cannot figure out a way to do this with multiple columns holding the same pattern. I am not very experienced and any help is appreciated.

import HTML

test_results = [
         70,
         50,
         20,
         5,
    ]

t = HTML.Table(header_row=['test'])
for new in sorted(test_results):

    #print new
    if new <=50:
        color = 'yellow'
    elif new <=100:
        color = 'blue'
    elif new <=150:
        color = 'green'
    elif new >150:
        color = 'white'
    colored_result = HTML.TableCell(new, bgcolor=color)

    t.rows.append([colored_result])
htmlcode = str(t)
print htmlcode 

This produces a single column table but I would like to add more data and have a table of many rows and columns.

This

t.rows.append([colored_result])

appends a complete row of one cell.

This

t.rows.append([colored_result, colored_result])

would append this cell twice, hence creating a row of 2 cells (identical).

This

colored_result = HTML.TableCell(new, bgcolor=color)
colored_result2 = HTML.TableCell(new, bgcolor='white')
t.rows.append([colored_result, colored_result2])

would append those two cells as a row in your table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM