#100DaysOfWeb in Python Transcripts
Chapter: Days 17-20: Calling APIs in Flask
Lecture: Passing our Chuck Norris joke to a Template

Login or purchase this course to watch this video and the rest of the course contents.
0:00 After we've created that little function called get chuck joke, we need to actually create the app.route for the chuck route.
0:10 And to do that, we go into routes.py as we have many times before and we're going to create yet another route here in our list.
0:19 But before we get started, because we have done requests down here, we do need to actually import that so import requests.
0:29 And following on from that, we can create our route. So, app.route. We're going to call the URL that we access this page on
0:40 Chuck, and we'll do the same for the function so def chuck. And all we are doing is returning a joke
0:49 and that joke is called using the get chuck joke function. So when this page is loaded or every time you load this page
1:02 you are going to run get chuck joke. That will run the code we ran in the last video to pull the joke down, it will assign that to the joke object here
1:14 and we are then going to return that joke object to our template, using the same thing we've done before which is render template.
1:23 So, we are going to create this page shortly so chuck.html is a file we will be talking to. And let's go across and joke=joke.
1:40 So, the joke=joke is similar to what we have up here in our date time, where we have the time now and then we're parsing that off
1:49 to their jinja template as time. In this instance, we are using the joke object here that we've just created here
1:57 and we're parsing it off, also using the exact same name to the Jinja template as joke. And that is all. So now that we have that, we can
2:08 let's just give ourselves an extra space here now that we have that, we can actually create the template so let's do that.
2:17 Right. Go into our templates folder. And we'll copy index.html, because it's pretty similar to what we want to do here with chuck.
2:26 So, copy index.html to chuck. Let's vim up chuck. And let's get rid of all of this. So we have our block content
2:37 this is the content unique to this page and we're extending again from base.html. Give ourselves a h1 tag, just as a nice little title
2:48 and we're going to call the page Chuck Norris Jokes. Right, and now what we have to do we have to take that joke object
3:01 the one that we just parsed off using the render template to this template. We are going to take that joke object
3:09 and simply print it within a paragraph tag. So we can put the P there for paragraph open our bracket there, throw a second bracket in
3:17 and notice again, the double left-hand brackets means we are about to reference a variable or an object
3:25 and we closed it off there, we're referencing the joke that we're parsing to our Jinja template and then we close it off.
3:34 And that's it. So we can save that now. So assuming I've actually done this right and that's a pretty big assumption
3:41 no I'm just kidding, let's run the code. And that's always a good sign that means nothing died yet. I say yet on purpose. So let's bring up the website
3:53 and here is our 100 days page, still up. Let's get rid of that. Let's open up Chuck and we have an error. And of course it's a nice little typo.
4:05 Instead of running chuck, I've written check. So let's get rid of that. And quickly replace that, make sure it all checks out. Get chuck joke, yes.
4:21 Let's cross our fingers and try again, Flask run. Let's go back to our page reload the page. And there we go.
4:34 So Check, Chuck Norris Jokes, geez, now I'm saying check. Chuck Norris can finish Mario Brothers without using the jump button.
4:42 So incredibly lame, I did not promise that these were going to be good jokes, so please don't hold it against me.
4:48 Now you can see, hopefully this next joke is okay. I'll leave that to you to read I'm not going to bother reading it.
4:58 But again, pretty silly, but you get the hang of it now. So the idea is every time we reload this page
5:05 we are running that code that was in our app.route decorator function for this page and that is to simply talk to the API
5:14 get a random joke, print it to the page. You can do this as many times as you want. And that is the general approach to hitting an API
5:23 using a Flask app.


Talk Python's Mastodon Michael Kennedy's Mastodon