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