#100DaysOfWeb in Python Transcripts
Chapter: Days 17-20: Calling APIs in Flask
Lecture: Passing variables between Flask app code and a Template
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Before we get started in actually playing with APIs we just need to quickly, quickly cover off how to pass variables from our Flask app
0:12
the actual Python code, through to that Jinja template to display it on the webpage. So right now we've just been passing off
0:20
standard text through print commands. This time we are actually going to pass off a variable that can change.
0:28
So in this example we're going to use the date. So using a date time string we're going to pass that off to the webpage and then display that.
0:39
And that's a great example because the date time string will change every time the page is loaded because the time keeps ticking, right?
0:47
So to do that let's edit first our routes dot PY file. That's where we're actually going to do the number crunching or the code so to speak.
1:04
Once we're in this file because we are going to actually put the date time returning on our index page that's where we want to display it.
1:15
We need to put our code within our index function. As this is just a simple date time though if you really think about it we just need
1:24
to return that string so there's not much that we need to do in here. All we really have to do to keep it super-simple
1:31
is import date time and then assign the time to a variable. So let's import datetime.now from datetime import datetime.
1:43
And then let's just create a little time object called time_now because we'll take the time_now. And then we will just quickly do datetime.today.
1:55
And what that will do is it'll get date time dot today, it assign it, it convert it to a string and then assign it to the time_now variable.
2:04
And then all we have to do is return that. So if you think about it we are returning with our render template index.html.
2:14
We are also going to pass off a variable. So that will go in these brackets here just after this one.
2:23
Now, just let me type this out before I explain it. Now, this looks a bit confusing but what we're doing is we're actually sending our time
2:34
now variable, this one here, to our Jinja template as a variable called time, as an object called time, okay?
2:43
You might see in other Flask apps where these are named the same thing. In other words, time_now equals time_now would be a traditional way of doing it
2:53
but just for demonstrative purposes I'm going to name them differently and that is so that you can see which variable is which.
3:02
So this one here, time_now, is the one that we're specifying up here in our Python script. And time is what we are going to call in our Jinja template.
3:12
All right, so that's all we have to do to update our routes dot PY file to send the date time object over to our Jinja template. So let's save that.
3:23
Let's head over to our templates folder. And let's have a look in index.html. As you can see from our previous Flask section
3:36
we have our index, everything that's unique to our index.html page, in between these block content tags.
3:43
And all we want to do is have a message that says that we are printing the time. So we can do that underneath here.
3:51
Let's just throw another p tag in or how about we throw a h1 tag here. The current date is, and as we learned
4:04
from the previous videos, anything that is Python code is going to be within these types of brackets and then we close it off here.
4:15
Now as I mentioned in the previous file the variable that we are sending off to our Jinja template is the word time, okay?
4:23
So we actually put in two of these brackets to indicate that we're playing with a variable here. I know it's hard to see with the highlighting
4:29
there so let's just space that out. And we throw in the word time. That's our variable. So because this is an actual object or a variable
4:38
that we are calling from our Python script it goes between these double curly brackets. And that's it. So the current date is, and then we can throw
4:49
in that time, and we save that, and we run our Flask app. And with that running let's bring up the browser and take a look.
5:04
All right, and here's our website running. We are on the 100 days page up here so let's click the home tab and this will initiate the index.html route.
5:14
And there we go, the current date is 2018 October the 11th, and there's our timestamp. Now, one thing you will notice is if we move
5:26
back to the 100 days page and then move back to our home tab, the actual time is not changing. And the reason this won't change
5:36
is because of where we've specified that assignment variable. If we just cancel our script we go into routes
5:47
dot PY and this is being specified at this level. It's being specified outside the route outside the index function.
5:58
And what that means is it is only being assigned when the Flask app is run. So if we cancel the Flask app and then rerun it
6:07
this will update with the time. All right, so that is why that won't change until you close your app and relaunch it.
6:14
If we actually want this to run every time that page is loaded, we have to put that within the index route, the index function here.
6:24
That way every time this is called it activates down here. So let's just copy and paste that. All right, there we go.
6:31
And right quick, Flask run, bring up our web browser. All right, back to the home tab. There's the time, 9:57 p.m. 23 seconds.
6:45
Launch it again, it's gone to 27 seconds. And that's how we can redo that every single time. And that is how we pass a variable off
6:56
to our Jinja template. Just remember we have the render template that we're returning and we're just specifying the variable there.
7:04
We put the actual Python variable on the right and we assign it to a variable that we want to call within the Jinja template.