#100DaysOfWeb in Python Transcripts
Chapter: Days 17-20: Calling APIs in Flask
Lecture: Working with the Chuck Norris API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we know how to pass variables over
0:03 to our Flask Jinja template
0:05 we're ready to start pulling data from an API.
0:08 The API that I've picked for this first attempt
0:12 is kind of nonstandard, but let's take a quick look.
0:17 There we have it, the Chuck Norris API.
0:19 This is a pretty simple and cool API.
0:24 Just for printing out Chuck Norris facts
0:27 or jokes, or however you want to look at it.
0:30 And it's as simple as, pointing to that URL there
0:35 to that endpoint.
0:37 All we have to do is do a request of that.
0:41 api.chucknorris.io/jokes/random
0:44 and that will return us a random json outputted joke.
0:49 So we'll have the icon URL, we'll have the id
0:52 the direct URL, and then the value.
0:55 And you can see by digging into all of this
0:58 the only thing that matters to us is
1:00 to return the value.
1:02 At this end of this, we are going to have
1:05 a webpage called "Chuck" that when you load it
1:09 will simply return a joke, a random joke
1:13 from this Chuck Norris API.
1:15 So back to our terminal here
1:17 we really need to do a little bit of planning, okay?
1:20 You need to visualize what you're going to do
1:24 before you just start writing code because
1:26 it helps you get a full understanding
1:27 of the picture that you're trying to paint.
1:30 In this instance, as I said, we're going
1:32 to be using the Chuck Norris API to get a joke
1:35 but in order to do that, we actually have
1:38 to split our code into two sections.
1:40 Remember, we have the part where Flask takes your joke
1:44 will take our joke, and pass it off
1:46 to the Jinja template, but then we also have
1:49 to pull the joke down as well.
1:50 So that's going to be two separate functions there.
1:53 One is going to be our decorated app dot route function
1:58 and the other one is going to be our get the joke function.
2:03 Alright, so to do that, let's jump into routes.py
2:11 and I think a great place to start would be
2:13 to actually get our joke first.
2:16 Now I've written this up in advance in the python shell
2:20 so I've tested it and it works.
2:22 You can just trust that when you write this
2:24 in it's going to be okay.
2:26 If not, feel free to actually open up python shell
2:30 and punch these commands in.
2:33 You can get them from the Github repo
2:35 and follow along that way.
2:37 As I said, we're calling it "Git Chuck Joke,"
2:44 and we're just going to do a standard requests dot git
2:48 and we're going to punch in that Chuck Norris end point there.
2:55 That's the one there, and that's it
2:57 and that will return that json app
3:00 that we saw on the webpage into the variable "r"
3:04 or the object "r", and then we just do I dot json
3:11 and we know that we're going to return the data value.
3:19 And what we've done there is we're returning the data
3:24 that belongs to the value tag in that json app
3:28 so we'll bring that back up
3:30 and we can see we have the value there.
3:33 So what we're doing is we're getting this
3:35 we're converting it to json, and then
3:38 we're decoding the json I should say
3:40 and then we are getting that data there
3:43 that is signed to the value key.