Search code examples
pythonflaskjinja2

render jinja2 template without a Flask context


I have a Flask application that calls flask.render_template without problems when it is invoked from a flask http request.

I need the same method to work outside of flask (from a python back end program)

resolved_template =  render_template(template_relative_path, **kwargs)

I could use the jinja2 api, but I would like the same method to work, in both contexts (flask and command line)


Solution

  • You need to render it in an app context. Import your app in your backend code and do the following.

    with app.app_context():
        data = render_template(path, **context)