freqtrade_origin/freqtrade/util/template_renderer.py

28 lines
898 B
Python
Raw Normal View History

2023-08-15 05:42:05 +00:00
"""
Jinja2 rendering utils, used to generate new strategy and configurations.
"""
2023-08-15 05:42:43 +00:00
2023-08-15 05:42:05 +00:00
def render_template(templatefile: str, arguments: dict = {}) -> str:
from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(
loader=PackageLoader('freqtrade', 'templates'),
autoescape=select_autoescape(['html', 'xml'])
)
template = env.get_template(templatefile)
return template.render(**arguments)
def render_template_with_fallback(templatefile: str, templatefallbackfile: str,
arguments: dict = {}) -> str:
"""
Use templatefile if possible, otherwise fall back to templatefallbackfile
"""
from jinja2.exceptions import TemplateNotFound
try:
return render_template(templatefile, arguments)
except TemplateNotFound:
return render_template(templatefallbackfile, arguments)