如何为HTML行着色?

时间:2016-06-18 00:23:15

标签: python html

我有以下代码来构建HTML行,现在当dict1中存在键时我想为行着色并且当dict2中的键出现另一种颜色时? 如何修改此代码以对行进行着色?

for item in jiradb :
    MailBody = MailBody + "<tr>"
    MailBody = MailBody + "<td>" + str(icount) + "</td>"
    print get_field(item, 'key')
    key = item['key']
    key_after_none_check = get_field(item,'key');
    crashid_link = "https://company.com/data/browse/" + key;
    key_present_in_anr_tombstone = False;
    if ((key in dict1) and (dict1[key] !=0)):
        key_present_in_anr_tombstone = True
        #MailBody "<a href=\"" + crashid_link + "\">" + str(get_field(item,'key ')) + "</a>;
        MailBody = MailBody + "<td>" + "<a href=\"" + crashid_link + "\">" + str(key_after_none_check) + "</a>" + "(" + "x" + str(dict1[key]) + ")" +  "</td>"
    if ((key in dict2) and (dict2[key] !=0)):
        key_present_in_anr_tombstone = True
        MailBody = MailBody + "<td>" + "<a href=\"" + crashid_link + "\">" + str(key_after_none_check) + "</a>" + "(" + "x" + str(dict2[key]) + ")" +  "</td>"
    if key_present_in_anr_tombstone == False:
        MailBody = MailBody + "<td>" + "<a href=\"" + crashid_link + "\">" + str(key_after_none_check) + "</a>" + "</td>"
    MailBody = MailBody + "<td style=\"width:100%\">" + get_field(item,'summary') + "</td>"
    MailBody = MailBody + "<td style=\"width:100%\">" + get_field(item,'Resolution') + "</td>"
    icount = icount + 1

MailBody = MailBody + "</tr>"

2 个答案:

答案 0 :(得分:0)

要解决您的问题,请先尝试获取值,然后使用css样式为行着色。

rows = []
for item in jiradb:
  data = []
  key = item['key']
  dict1_value = dict1.get(key)
  dict2_value = dict2.get(key)
  key_after_none_check = get_field(item,'key')
  crashid_link = "https://company.com/data/browse/{}".format(key)

  row = '<td><span style="background-color:{};"><a href="{}">{}</a>(x{})</span></td>'
  color = 'white' # default color

  if dict1_value != None:
    color = 'red';
    data.append(row.format(color, crashid_link, key_after_none_check, dict1_value))
  if dict2_value != None:
    color = 'blue'
    data.append(row.format(color, crashid_link, key_after_none_check, dict2_value))

  if not dict1_value and not dict2_value:
    data.append('<td><a href="{}">{}</a></td>'.format(crashid_link, key_after_none_check))

  data.append('<td style="width:100%">{}</td>'.format(get_field(item, 'summary')))
  data.append('<td style="width:100%">{}</td>'.format(get_field(item, 'Resolution')))
  rows.append(data)

mail_rows = []
for count,row in enumerate(rows):
  head = '<tr><td>{}</td></tr>'.format(count+1)
  body = '\n'.join(row)
  footer = '</tr>'
  mail_rows.append('{}{}{}'.format(head,body,footer))

mail_body = ''.join(mail_rows)

当前输出: -

rows

答案 1 :(得分:0)

你的代码非常混乱。我不确定我是否做得好,但看看这个版本。我不知道icount来自哪里,但你可以从枚举中得到它。我创建了一个字符串来包含我认为是表行的模板。然后我在if子句中取出了所有重复逻辑。收集值,然后将它们弹出到模板中。

for icount, item in enumerate(jiradb) :
    vars = []
    MailBody = MailBody + 
        """<tr><td>{0}</td>
           <td><a href="https://company.com/data/browse/{1}">{2}</a>(x{3})</td>
           <td style='width:100%'>{4}</td>
           <td style='width:100%'>{5}</td></tr>
        """
    vars.append(icount+1)

    key = item['key']
    vars.append(key)
    key_after_none_check = get_field(item,'key');
    vars.append(key_after_none_check)
    key_present_in_anr_tombstone = False;

    if ((key in dict1) and (dict1[key] !=0)):
        key_present_in_anr_tombstone = True
        vars.append(dict1[key])

    if ((key in dict2) and (dict2[key] !=0)):
        key_present_in_anr_tombstone = True
        vars.append(dict2[key])

    if key_present_in_anr_tombstone == False:
        vars.append(key_after_non_check)

    vars.append(get_field(item, 'summary'))
    vars.append(get_field(item, 'Resolution'))

    MailBody.format(*vars)