【问题标题】:Change the bg color dynamically, instead of using static value动态改变背景颜色,而不是使用静态值
【发布时间】:2019-04-12 20:01:25
【问题描述】:
def raw_dp(value, loop_name, break_point_1, break_point_2, break_point_3, break_point_4, break_point_5, metrics, symbol = ''):

    facts = [(round(item)) for item in value]
    metrics  += '''<tr><th>'''+ loop_name +'''</th>'''
    for dp in facts:
        if dp <= break_point_1:
            metrics += '<td style = "background-color:#229954">' + ' ' +  str(dp)  + symbol + '</td>'
        elif dp > break_point_1 and dp <= break_point_2:
            metrics += '<td style = "background-color:#ABC878">' + ' ' +  str(dp)  + symbol + '</td>'
        elif dp > break_point_2 and dp <= break_point_3:
            metrics += '<td style = "background-color:#F9E79F">' + ' ' +  str(dp)  + symbol + '</td>'
        elif dp > break_point_3 and dp <= break_point_4:
            metrics += '<td style = "background-color:#FBD567">' + ' ' +  str(dp)  + symbol + '</td>'
        elif dp > break_point_4 and dp <=break_point_5:
            metrics += '<td style = "background-color:#F9C169">' + ' ' +  str(dp)  + symbol + '</td>'
        else:
            metrics += '<td style = "background-color:#E67C73">' + ' ' +  str(dp)  + symbol + '</td>'
    metrics += '''</tr>'''
    return metrics

我想动态更改 html 单元格的背景颜色,而不是使用我定义为 breakpoint_1、breakpoint_2...等的静态值或任何其他优雅地编写此函数的方式。我是python新手,谁能帮帮我。

【问题讨论】:

    标签: python html python-3.x python-2.7 function


    【解决方案1】:

    我不做 Python,但我建议实现一个案例

    https://jaxenter.com/implement-switch-case-statement-python-138315.html

    def switch_demo(argument):
      switcher = {
        0: "229954",
        1: "229954",
        2: "ABC878",
        3: "F9E79F",
        4: "FBD567",
        5: "F9C169"
      }
    

    使用类似的东西

    bg = switcher.get(argument,"E67C73")
    

    这里你需要将测试变成一个从 0 到 5 的数字——正如我所说的我不会使用 Python,但字典应该是一个可能的解决方案

    如果不行,那就试试

                                                       bg = "E67C73"
     if   dp <= break_point_1:                         bg = "229954"
     elif dp >  break_point_1 and dp <= break_point_2: bg = "ABC878"
     elif dp >  break_point_2 and dp <= break_point_3: bg = "F9E79F"
     elif dp >  break_point_3 and dp <= break_point_4: bg = "FBD567"
     elif dp >  break_point_4 and dp <= break_point_5: bg = "F9C169"
    

    然后有一个

    metrics += '<td style = "background-color:#' + bg + '">' +  str(dp)  + symbol + '</td>'
    metrics += '''</tr>'''
    

    不需要+ ' ',因为在 HTML 中被忽略了。而是设置单元格填充

    【讨论】:

    • 你怎么能检查上面的条件,我认为它不会起作用
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2019-09-10
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多