Make jinja2 template without jar context

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

I need the same method to work outside the jar (from the end program in python)

 resolved_template = render_template(template_relative_path, **kwargs) 

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

+7
python flask jinja2
source share
3 answers

You need to display it in the context of the application. Import the application into your code and do the following.

 with app.app_context(): data = render_template(path, **context) 
+7
source share

If you want to completely bypass flask and use pure Jinja to render your template, you can do as such

 import jinja2 def render_jinja_html(template_loc,file_name,**context): return jinja2.Environment( loader=jinja2.FileSystemLoader(template_loc+'/') ).get_template(file_name).render(context) 

And then you can call this function to render html

+7
source share

I am using this code:

 import jinja2 template_values = { 'value_name_in_html': value_name_in_python, } template = JINJA_ENVIRONMENT.get_template("file_patch") self.response.write(template.render(template_values)) 
0
source share

All Articles