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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With