Check out a free preview of the full Intermediate Python course:
The "Templates & Other Resources" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to begin using the templating system in Flask, and gives resources for those wanting to continue learning about Flask.

Get Unlimited Access Now

Transcript from the "Templates & Other Resources" Lesson

[00:00:00]
>> Nina Zakharenko: It would be great if the web was all plain text, but it's not. So, Flask provides something called templating, which allows us to quickly and easily access variables without having to use JavaScript or anything fancy. In order to use a template in whichever folder your hello.py is, we're gonna open the terminal.

[00:00:33] We can kill our server for now just so we know we're on the right path. We're gonna make a new directory and we're gonna call it templates.
>> Nina Zakharenko: And then we're gonna touch a file here, index.html. And-
>> Nina Zakharenko: That touch command won't work for Windows. So let me quickly, yeah, I believe it's F-C.

[00:01:02] And then a forward slash. So for Windows it's, or not a forward slash, it's a bracket templates/index.html, okay?
>> Nina Zakharenko: To get our simple template to work, we have to do a few things. The first is we need to import render_template from Flask.
>> Nina Zakharenko: And render_template takes-
>> Nina Zakharenko: The name of the template that we want to use.

[00:01:49] So here, instead of returning hello_world, we're gonna return render_template, and we're gonna pass in the name of our template, we don't have to put in the template's directory. Now let's open our index.html file and put, this is a template, and save that.
>> Nina Zakharenko: After you're done creating the files, remember to run your Flask server again.

[00:02:22] And now if I click on this link, there we go, got, this is a template!. Is everyone seeing the same thing? Flask supports a templating language called Jinja that allows you to populate your HTML files with data from your Flask app at render time. I'm not going to go into the specifics of Jinja, but I will show you a-

[00:02:53]
>> Nina Zakharenko: Simple example.
>> Nina Zakharenko: So let's just make a, let's put some HTML texts in our template and make the title of our page be, Hello, from Flask! We can delete our this is a template. So, in order for our template to have variables, we need to explicitly pass them in.

[00:03:22] The way that we would do that is via this render template. So the first argument is the name of the template, and then any other arguments after that get passed into the template. So I can say, the name is Nina, and then pass it in here in the call to render template.

[00:03:43]
>> Nina Zakharenko: Going back to our template file, we now have access to this variable. And so this Jinja templating language looks like this. The arguments are within a curly brace percent sign. So if you've used templating for other languages, like JavaScript, this shouldn't look too far-fetched. And so, I can put simple expressions in here.

[00:04:07] If you wanna know more about Jinja I would check out the documentation. But I can say if name-
>> Nina Zakharenko: And with Jinja, Jinja is just like HTML, so we need to have opening tags and closing tags to signify a block of code. So to close an if statement in Jinja, you would just say endif.

[00:04:33]
>> Nina Zakharenko: Now we can put some conditional code between these two texts. I can say hello in each one. And then to access the value of a variable, you just use two sets of curly braces, and this will print out the value of name. Let's reload our web page, and we'll see the information from our new template.

[00:04:57] So everyone see that?
>> Nina Zakharenko: Great! So, just a little bit more about Flask. Like I mentioned, Flask, any of these other web frameworks, they're really easy to add to. So if your app is ready for the big time and you want to add a database, Flask is a useful mechanism for accessing database objects.

[00:05:24] It makes it really easy to use databases and store your data for a dynamic web app. You can find a lot more information about that in the Flask documentation, but unless you want to be here until midnight it's a topic for another day. In terms of deploying your web app, there are a lot of different options from beginner to advanced.

[00:05:48] To deploy your real web server, I think Heroku is the best bet for beginners. It's pretty easy, it provides a lot of step by step instructions, and then as your app grows you can graduate from there. The Flask documentation list other deployment options. And know that we're covering just the basics of Flask today.

[00:06:10] But to deep dive and learn more, the Flask Mega-Tutorial is a great resource to check out after this class. It's a huge tutorial that covers everything from databases to to time zones. Probably take you a few days to complete, and you'll feel pretty comfortable with Flask after that.