Color coding cells in a table based on the cell value using Jinja templates

后端 未结 2 1608
梦如初夏
梦如初夏 2020-12-19 12:39

I have a simple flask app and need to display a table of values, with the cell backgrounds colour coded based on the cell value according to thresholds. I\'m generating the

相关标签:
2条回答
  • 2020-12-19 13:20

    The easiest way would be to put this display logic in your template:

    <table>
        {% for row in data %}
        <tr>
            {% for item in row %}
                {% if item <= 10 %}
                    <td class="under-limit">{{ item }}</td>
                {% else %}
                    <td>{{ item }}</td>
                {% endif %}
            {% endfor %}
        </tr>
        {% endfor %}
    </table>
    

    Then, in your CSS you can use:

    .under-limit { background-color: red; }
    
    0 讨论(0)
  • 2020-12-19 13:30
    <table>
       {% for row in row %}
           {% if item <= 10 %}
           <tr style ="background-color: red">
               <td> {{ item }} </td>
           </tr>
           {% else %}
           <tr>
               <td> {{ item }} </td>
           </tr>
           {% endif %}
        {% endfor %}
    </table>
    

    This works for me.

    0 讨论(0)
提交回复
热议问题