votes up 4

Can't compile non template nodes

Package:
pipenv
github stars 22232
Exception Class:
TypeError

Raise code

def generate(
    node, environment, name, filename, stream=None, defer_init=False, optimized=True
):
    """Generate the python source for a node tree."""
    if not isinstance(node, nodes.Template):
        raise TypeError("Can't compile non template nodes")
    generator = environment.code_generator_class(
        environment, name, filename, stream, defer_init, optimized
    )
    generator.visit(node)
    if stream is None:
        return generator.stream.getvalue()
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 0 votes down

Error code:

from pipenv.vendor.jinja2.environment import Environment
from jinja2 import Template

#Create template
template = Template('Hello {{ name }}!')
#Create environment
env = Environment()
#Compile template 
comp = env.compile(template)
print(comp)

Environment is the core component of Jinja. It contains important shared variables like configuration, filters, tests, globals, and others. Environment.compile is used to compile node or template source code. compile method takes as an argument source which is a Template or string. But in the above code template variable is binary which indicates the location in the memory. That's why an error is raised.

Fix code:

from pipenv.vendor.jinja2.environment import Environment
from jinja2 import Template

#Create template
template = Template('Hello {{ name }}!')
#Render template so variable an become string
an = template.render(name='John Doe')

#Create environment
env = Environment()
#Compile template 
comp = env.compile(an)
print(comp)

Jul 23, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix