'how to call a global variable in a function in flask?

enter image description here

Hello guys, small part of my code here, i have result and pred variables. And in the html part i have this code:

<div style = "margin-Top:100px;" class="container">
    
    {{ results[pred] }}

</div>

And i get this error. So how can i fix it? So i thought the variable in html calling like that in flask. Am i wrong?



Solution 1:[1]

results[pred] uses 2 variables to resolve:

  • results, dict
  • pred, the lookup in the dict

It seems, that u only need the value of pred in results. You can do this by using return render_template('index.html', var=results[pred]) and {{ var }}

Edit: In your case pred is static in python so you should only return the mandotory value of the results dict to the template.

Solution 2:[2]

You can’t pass parameter like this.

In render_template pass result=result

And then in the template you can use it like you write - result[pred]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 administratar
Solution 2 TeRe