Z
Z
Zagir Majidov2022-03-08 19:05:01
Flask
Zagir Majidov, 2022-03-08 19:05:01

How to call python command in html using jinja2?

I want to call trans command in

app.py html file:

def trans(txt): # функция для вызова
   return txt

index.html:
{{ trans('test') }} // Вызываю функцию, но нечего не происходит

How can such functionality be made?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-03-08
@Zagir-vip

well, let me read the docks for you https://flask.palletsprojects.com/en/2.0.x/templat...

A
Alexander Nesterov, 2022-03-08
@AlexNest

Well, at least for starters, pass it to the context:

def foo():
    return "foo() called"

Template("{{ foo() }}").render(foo=foo)

On the other hand, the need to call the trans function in the template is not at all clear. Isn't it easier to specify txt right away ? Well, or write the return value to a variable and pass it already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question