import os

def render_template(template_name, **kwargs):
    from jinja2 import Environment, FileSystemLoader

    env = Environment(loader=FileSystemLoader('templates'))
    tmpl = env.get_template(template_name)

    return tmpl.render(**kwargs)

import os

_env = None
def render_template(template_name, **kwargs):
    from jinja2 import Environment, FileSystemLoader
    global _env

    if _env is None:
        _env = Environment(loader=FileSystemLoader('templates'))

    tmpl = _env.get_template(template_name)
    return tmpl.render(**kwargs)

Use global Jinja2 environment in render_template
Use global Jinja2 environment in render_template

Before this patch, a new environment was created in every call of
render_template().

  import os
  
+ _env = None
  def render_template(template_name, **kwargs):
      from jinja2 import Environment, FileSystemLoader
+     global _env
  
+     if _env is None:
-     env = Environment(loader=FileSystemLoader('templates'))
+         _env = Environment(loader=FileSystemLoader('templates'))
-     tmpl = env.get_template(template_name)
  
+     tmpl = _env.get_template(template_name)
      return tmpl.render(**kwargs)
  
Use global Jinja2 environment in render_template
## Code Before:
import os

def render_template(template_name, **kwargs):
    from jinja2 import Environment, FileSystemLoader

    env = Environment(loader=FileSystemLoader('templates'))
    tmpl = env.get_template(template_name)

    return tmpl.render(**kwargs)

## Instruction:
Use global Jinja2 environment in render_template
## Code After:
import os

_env = None
def render_template(template_name, **kwargs):
    from jinja2 import Environment, FileSystemLoader
    global _env

    if _env is None:
        _env = Environment(loader=FileSystemLoader('templates'))

    tmpl = _env.get_template(template_name)
    return tmpl.render(**kwargs)
