Transcript from the "Templates & Other Resources" Lesson
[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-
>> 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: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.
>> 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.