Импортировать библиотеки Python в шаблоны jinja2Python

Программы на Python
Anonymous
Импортировать библиотеки Python в шаблоны jinja2

Сообщение Anonymous »

У меня есть этот шаблон:

Код: Выделить всё

% template.tmpl file:
% set result = fractions.Fraction(a*d + b*c, b*d) %}
The solution of ${{a}}/{{b}} + {{c}}/{{d}}$ is ${{a * d + b*c}}/{{b*d}} = {{result.numerator}}/{{result.denominator}}$
который я вызываю

Код: Выделить всё

from jinja2 import Template
import fractions

with open("c.jinja") as f:
t = Template(f.read())
a = 2
b = 3
c = 4
d = 5
print(t.render(a = a, b = b, c=c, d=d))
Я понимаю

Код: Выделить всё

jinja2.exceptions.UndefinedError: 'fractions' is undefined
но я хочу

Код: Выделить всё

The solution of $2/3 + 4/5$ is $22/15=22/15$.
Можно ли этого добиться?

Подробнее здесь: https://stackoverflow.com/questions/786 ... -templates

Вернуться в «Python»