from jinja2 import Environment, FileSystemLoader, select_autoescape from pathlib import Path def get_env(templates_dir: str): here = Path(__file__).resolve().parent searchpaths = [templates_dir, str(here / "templates"), str(Path().resolve() / "templates")] env = Environment(loader=FileSystemLoader(searchpaths), autoescape=select_autoescape(["html","xml"])) return env def render(env, template_name: str, context: dict) -> str: template = env.get_template(template_name) return template.render(**context)