I am creating my first small web app using the Flask framework and I am not sure which approach is best practice to change the properties of elements (ID='#NRG') on my page based upon data defined in the Python code on the backend:
Flask / Jinja2 Approach:
{% if nrg_precip_probs[0] <=25 and nrg_precip_probs[1] <= 25 and nrg_precip_probs[2] <=25 %}
<script>$("#NRG").css("background", "#21CE99");</script>
{% else %}
<script>$("#NRG").css("background", "#F45531");</script>
{% endif %}
or a JavaScript Approach:
function change_it() {
if ({{nrg_precip_probs[0]}} <= 25) {
$("#NRG").css("background", "#21CE99");
} else {
$("#NRG").css("background", "#F45531");
}
}
change_it();
ifstatement should be done at the Flask route function and pass in the color attribute as part of the template context to the Jinja template so that it can be rendered, e.g.<div id="NRG" style="background: {{ color }}">