Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colored text in Jinja2 template depending on condition in Python

Tags:

python

jinja2

I have the Jinja2 template

<!DOCTYPE html>
<html lang="en">
    <body>
        <table style="width:30%">
            {% for key, value in forces.get_max_allowable_force_per_edge_exceeded_or_not().items() %}
                <tr>
                    <td>{{key}}</td>
                    <td>{{"{:+.2e}".format(value[0])}} * N</td>
                    <td>{{"I want this text in green!" if value[2] == False else "I want this text in red!"}}</td>
                </tr>
            {% endfor %}
        </table>
    </body>
</html>

in which I would like to give "I want this text in green!" the color green if value[2] == False and red otherwise.

How do I achieve this?

like image 509
Adriaan Avatar asked Oct 18 '25 06:10

Adriaan


1 Answers

You can use a condition to set a class on the table cell.

<td class="{{'red' if value[2] == True else 'green'}}">Text</td>

<style>
    .red {
        color: red;
    }

    .green {
        color: green;
    }
</style>
like image 126
tomasantunes Avatar answered Oct 20 '25 20:10

tomasantunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!