Change the style or background of a cell in Dominate table (Python)
Question: Here's a sample from my csv file (imagine that the xxxx.img are actually
<http://my.website.me/xxxx.img>)

    
    
    LHS_itemname,LHS_img, LHS_color, RHS_itemname, RHS_img, RHS_color 
    backpack,    bck.img, blue     , lunchbox,     lch.img, blue
    backpack,    bck.img, green    , lunchbox,     lch.img, blue
    

I want to display this csv as an HTML table where each image url can be
grabbed from the web using the web url and displayed inside the table. And if
the LHS_color is the same as the RHS_color, I want that row in the table to
have a grey background.

Here's what I have so far using the `dominate` package in Python:

    
    
    import os
    import os.path
    import sys
    import csv
    import urllib
    import re
    import glob
    import numpy as np
    from dominate import document
    from dominate.tags import *
    import dominate
    

Set names for the input csv and output html (call them inFileName, and
outFileName)

    
    
    f = open(inFileName, 'rb')  # Path to csv file
    reader = csv.reader(f)
    header = ['LHS_itemname','LHS_img', 'LHS_color', 'RHS_itemname', 'RHS_img', 'RHS_color']
    with document(title='ItemsBoughtTogether') as doc:
    h1('ItemsBoughtTogether', align = 'Center')
    with table(border='1').add(tbody()):
    
        l = thead().add(tr())
        for col in header:
            print col
            l += td(p(b(str(col))))
    
        l = thead().add(tr())
        for row in reader:
            l = tr()
            l += td(p(row[0], ALIGN='Center'))
            l += td(p(row[1], ALIGN='Center'))
            l += td(div(img(src=row[2]), _class='photo', ALIGN='Center')) # img LHS
            l += td(p(row[3], ALIGN='Center'))
            l += td(p(row[4], ALIGN='Center'))
            l += td(div(img(src=row[6]), _class='photo', ALIGN='Center')) # img RHS
            if row[2] == row[5]: {background-color:'grey'}
    

This last `if` statement is what I don't know how to put in syntactically. I'm
having a hard time finding dominate examples with html tables in general, so
if anyone has good resources for that, please comment.


Answer: I've never used dominate, but it's generally preferable to use style sheets
for css attributes (like background colour). I would just include an external
style sheet here, and give this row a certain class if it satisfies your
criteria.

eg. style.css:

    
    
    .grey_background {
        background-color: grey;
    }
    

add in a link (after the `with document(title...` line:

    
    
    with doc.head:
        link(rel='stylesheet', href='style.css')
    

finally, add the class - instead of: `l = tr()`, do something like:

    
    
    l = tr(_class='grey_background') if row[2] == row[5] else tr()
    

**Edit: Alternatively, for an inline style**

Since it seems to support keywords, the following should work:

    
    
    l = tr(style="background-color: grey") if row[2] == row[5] else tr()
    

