Jinja2: A Medium Between Python and Front End

Jinja2: A Medium Between Python and Front End

I first looked at Flask and Django because people said that if I want to use Python on a webpage I’d need a web framework. Turns out, the simpler thing to learn is not a web framework but a template engine.

What is a template engine? Who the hell knows? Do you think I knew as a beginner? Why do all these tutorials call it that? Tutorials for beginners, no less. And if you want to even learn one, Jinja2 in my case, you can barely find a tutorial that focuses on it. Most Jinja2 tutorials involve a great deal of Flask.

So here’s the thing, Jinja2 is the medium that weaves between Python and a webpage on the front end. It allows us to show results from a Python function in the back end on a web page. It can’t do everything but, sometimes, it’s more than enough for us.

As far as I know, Jinja2 remains an integral part of the Holy Trinity of Flask-Jinja2-Werkzeug. Pip install Flask would get you the entire Trinity. However, you don’t need to learn all of Flask to put Python functions on the web. You do still need to import flask in your script and learn the basics of where to put your files and how to name your folders the way Flask likes it, but after that, it can be simple. Here’s an example.

In your index.html file, which must be placed in the templates folder according to Flask’s preferences, you can have a string like, say:

I like {{type_of_jam}}.

In your server script, you can do:

@app.route(‘/’)
def mainIndex():
list = [‘butter’, ‘apple jam’, ‘orange marmalade’, ‘crude oil’]
jam = random.choice(list)
return render_template(‘index.html’, type_of_jam=jam)

And that’s it. Your string on the webpage randomly displays a type of jam.

You can do other things with it, of course, and you’ll need some more knowledge to better appreciate what it can do. But my point is, for beginners to get to here, we first need to tunnel through mountains of other stuff, a lot of which is unnecessary (even if nice) simply because the current ecology of pedagogical resources don’t design for noobs.

Instructions Unclear, Stuck Inside Washing Machine Inside Blender

Instructions Unclear, Stuck Inside Washing Machine Inside Blender

The trouble with Code Academy is that its instructions aren’t clear a lot of the time. For instance, it gives the following Javascript code:

var understand = true;

while( FILL THIS BLANK ){
console.log(“I’m learning while loops!”);
understand = false;
}

And says, “complete the while loop… Do this by adding the condition between the parentheses”. The clue: “As long as the condition evaluates to true, the loop will continue to run. As soon as it’s false, it’ll stop. (When you use a number in a condition, as we did earlier, JavaScript understands 1 to mean true and 0 to mean false.)”

When a novice reads this, he/she thinks, I can put anything True in there! So, I crashed the browser three times before I realised it wants me to put the variable “understand” in there.

This lack of clarity can get quite frustrating if it makes you spend an hour or more trying to interpret the vagueness rather than working on code. It happens enough to be noticeable, but I’m patient so I’ll slog on.