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
|
|
|
|
2024-04-19 05:24:11 +00:00
|
|
|
|
2024-10-04 05:08:30 +00:00
|
|
|
def render_template(templatefile: str, arguments: dict) -> str:
|
2023-08-15 05:42:05 +00:00
|
|
|
from jinja2 import Environment, PackageLoader, select_autoescape
|
|
|
|
|
|
|
|
env = Environment(
|
2024-05-12 14:56:05 +00:00
|
|
|
loader=PackageLoader("freqtrade", "templates"),
|
|
|
|
autoescape=select_autoescape(["html", "xml"]),
|
2023-08-15 05:42:05 +00:00
|
|
|
)
|
|
|
|
template = env.get_template(templatefile)
|
|
|
|
return template.render(**arguments)
|
|
|
|
|
|
|
|
|
2024-05-12 14:56:05 +00:00
|
|
|
def render_template_with_fallback(
|
2024-11-07 20:37:33 +00:00
|
|
|
templatefile: str, templatefallbackfile: str, arguments: dict | None = None
|
2024-05-12 14:56:05 +00:00
|
|
|
) -> str:
|
2023-08-15 05:42:05 +00:00
|
|
|
"""
|
|
|
|
Use templatefile if possible, otherwise fall back to templatefallbackfile
|
|
|
|
"""
|
|
|
|
from jinja2.exceptions import TemplateNotFound
|
2024-05-12 14:56:05 +00:00
|
|
|
|
2024-04-19 05:26:35 +00:00
|
|
|
if arguments is None:
|
|
|
|
arguments = {}
|
2023-08-15 05:42:05 +00:00
|
|
|
try:
|
|
|
|
return render_template(templatefile, arguments)
|
|
|
|
except TemplateNotFound:
|
|
|
|
return render_template(templatefallbackfile, arguments)
|