Intermediate Python

Templates & Other Resources

Intermediate Python

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.


Transcript from the "Templates & Other Resources" Lesson

>> 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 is, we're gonna open the terminal.

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.

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.

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.

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-

>> 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.

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.

>> 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.

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.

>> 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.

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.

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.

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.

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.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now