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.

Path of Least Resistance: From Knowing Nothing to Python to Flask

Path of Least Resistance: From Knowing Nothing to Python to Flask

I have to work on urgent stuff so I can’t go on with Flask for the moment. 😦 But I want to quickly pen down what I think might be the relatively easiest way to go from knowing nada to learning to make a web app, based on my experience.

Code Academy

Easy stuff. Dip your feet in shallow waters with its Python course. Also do its HTML, CSS, and Javascript stuff.

Learn Python the Hard Way (LPTHW)

First half was good, second half got too hard, partly because the way things were explained did not agree with me. I quit this site in its last third, I think.

PythonProgramming.net

I think I did Python basics on this site in the same period I did LPTHW. I may have done one or two other basic tutorials too because I wanted strong enough fundamentals to make learning the harder stuff easier later.

I don’t think my fundamentals are strong even now though. For instance, I’ve never written a class and I’m not sure I know how to.

Web Design for Everybody (Basics of Web Development and Coding), with Colleen van Lent

Lent is very clear on most things. I also did a Bootstrap course somewhere online but I can’t remember what it was. Learning front end stuff helps me understand which code comes from what programming language and where it should go when doing Flask. Learning Flask will involve Python, SQL, HTML, CSS, and JS, at some point or other.

I didn’t do the Capstone for this or any other Coursera course I did, because it costs money.

Python for Everybody, with Charles Severance

The first couple of Python courses in this specialisation are pretty all right. But I feel beginners require a lot more hands-on/embodied experience before we know to use Stack Overflow and other resources as vital supplements to understanding the things instructors miss out, and to troubleshoot all kinds of problems course instructors could not predict might affect their students. This is why it’s a good idea to go through Code Academy and LPTHW before doing this Coursera specialisation.

Severance is the clearest instructor I’ve come across on the things he goes through in the “Using Python to Access Web Data” and “Using Databases with Python” courses. I gave up working on a couple of the more complicated projects towards the end, in part because the instructions grew less and less and I kind of still needed them. But most of the courses are really pretty damn great.

Templating With Jinja2 in Flask

I didn’t finish even midway through part 3 of this series but the first two parts gave me a pretty ok beginner grasp of Flask with respect to Jinja2.

CS350 Flask tutorials on YouTube

For me, the clearest instructor and content so far when it comes to Flask. I’m slightly less than halfway through at the moment, though, I think.

UPDATE 19 April 2017: Model-View-Controller (MVC) Paradigm

At this point, I feel, it’s time to focus on two things: security, and the MVC paradigm. I am presently trying to figure out flask-security, but haven’t found good enough teaching/learning material for it. Please message me if you know some.

For MVC, this is a good introduction:

It helped me a lot, made me realise I could reduce my code by around 90%.

Others

Throughout this journey, Stack Overflow has been absolutely vital for finding solutions to miscellaneous problems. But usually, I google my problems and it finds solutions on Quora and a lot of other great websites too. I tried duckduckgo but its results were dismal for research.

UPDATE 2 April 2017: Yes, CS350 is good, but slightly more than halfway through it, I found myself building my own web apps, and that meant that I began seeking solutions from everywhere. It was a sudden and significant decentralisation of sources. A bit more detail in Part 3 of my experience learning Flask.