1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

yes this is working to append new rows, however, the information is exactly the same, I don't know how to have my next set of numbers in another row it just doesn't make sense to me. All my numbers are coming from 'new' which is the loop that created the colors but I need more data than that it's all very confusing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.