Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import and call a Python function in a Jinja template? [closed]

I want to use a function from a python module, in my Jinja template inside a Flask application. How do I import the module or pass the function to the template so that I can use it?

like image 471
firstlegagain1 Avatar asked May 26 '17 16:05

firstlegagain1


1 Answers

First approach. If you want to pass function only to one template, you can pass function to template as variable. For example

render_template("index.html", func=f)

And then call it in template, {{ func(1) }}

If you want function or variable accessible in all templates (globally). You can add them during flask application initialization to app.jinja_env.globals dict, like:app.jinja_env.globals['func'] = f

like image 123
varela Avatar answered Nov 07 '22 20:11

varela