Não reinvente a roda

É má prática, mocinho!

from flask import Flask, request
from jinja2 import Environment, PackageLoader, select_autoescape


env = Environment(
    loader=PackageLoader("app"),
    autoescape=select_autoescape(),
)

app = Flask("testapp")


# Make a dry run to create a custom template for logs. Outputs a
# example of the rendered template.
@app.route("/logs/templates/dry", methods=["POST"])
def root():
    request_data = request.json
    template = env.from_string(
        request_data["content"],
        globals=request_data["global_variables"],
    )

    return template.render(
        {
            "app": {
                "name": "Test APP",
                "url": "https://test-app.example.com",
                "port": 443,
                "https_enabled": True,
            },
            "errors": [
                {
                    "message": "Something went wrong!",
                    "stacktrace": [
                        "...",
                    ],
                },
            ],
        }
    )