1

Am having a Flask app which receives json data.This is the json format defined in views.py.

Values = [
    {
        'Count':0,
        'RPM':0,
        'ECT':0
    },
    {
        'Count':1,
        'RPM':1,
        'ECT':1
    }
]

Each updates json data is passed to html also as argument

@app.route("/members")
def members():
    return render_template("members.html",VALS=Values)

Inside the html page the json data is treated like this

{% for VAL in VALS %}            
    {% if (VAL['ECT'] > 251) %}
        <h1>  -> RPM:,ECT:{{VAL.ECT}} <button type="button" class="btn  btn-danger btn-sm">High</button> </h1>
    {% else %}
        <p> {{VAL.Count}} -> RPM:{{VAL.RPM}},ECT:{{VAL.ECT}} <button type="button" class="btn btn-success btn-md">Normal</button> </p>
    {% endif %}
{% endfor %}

I am facing problems with checking condition in if.The condition

{% if (VAL['ECT'] > 251) %}

is not working. How can i solve this?

2 Answers 2

2

Found the answer

{% if (VAL.get('ECT')|int > 251) %}

This one will work.Need to convert that to int :)

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the parentheses in Jinja2 syntax.

{% if VAL.get('ECT') > 251 %} 
<!-- do stuff -->
{% endif %}

Or even {% if VAL.ECT > 251 %}.

That format should be sufficient, if you pass a dict into the template from your view. However, if you are passing in JSON, everything is flattened to a string, so you specifically need to filter the value to an int:

{% if VAL.ECT|int > 251 %}<!-- do stuff -->{% endif %}

2 Comments

i tried both of your solution but its not working if we use VAL.ECT will it be a string value so does comparing with int value works?
Ah of course - if you pass the values in as a Python dict to the template, it will work, but JSON is flattened to a string. You need the filter as you showed in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.