#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 to our Flask Jinja template we're ready to start pulling data from an API.
0:09 The API that I've picked for this first attempt is kind of nonstandard, but let's take a quick look. There we have it, the Chuck Norris API.
0:20 This is a pretty simple and cool API. Just for printing out Chuck Norris facts or jokes, or however you want to look at it.
0:31 And it's as simple as, pointing to that URL there to that endpoint. All we have to do is do a request of that. api.chucknorris.io/jokes/random
0:45 and that will return us a random json outputted joke. So we'll have the icon URL, we'll have the id the direct URL, and then the value.
0:56 And you can see by digging into all of this the only thing that matters to us is to return the value. At this end of this, we are going to have
1:06 a webpage called "Chuck" that when you load it will simply return a joke, a random joke from this Chuck Norris API. So back to our terminal here
1:18 we really need to do a little bit of planning, okay? You need to visualize what you're going to do before you just start writing code because
1:27 it helps you get a full understanding of the picture that you're trying to paint. In this instance, as I said, we're going
1:33 to be using the Chuck Norris API to get a joke but in order to do that, we actually have to split our code into two sections.
1:41 Remember, we have the part where Flask takes your joke will take our joke, and pass it off to the Jinja template, but then we also have
1:50 to pull the joke down as well. So that's going to be two separate functions there. One is going to be our decorated app dot route function
1:59 and the other one is going to be our get the joke function. Alright, so to do that, let's jump into routes.py
2:12 and I think a great place to start would be to actually get our joke first. Now I've written this up in advance in the Python shell
2:21 so I've tested it and it works. You can just trust that when you write this in it's going to be okay.
2:27 If not, feel free to actually open up Python shell and punch these commands in. You can get them from the Github repo and follow along that way.
2:38 As I said, we're calling it "Git Chuck Joke," and we're just going to do a standard requests dot git
2:49 and we're going to punch in that Chuck Norris end point there. That's the one there, and that's it and that will return that json app
3:01 that we saw on the webpage into the variable "r" or the object "r", and then we just do I dot json
3:12 and we know that we're going to return the data value. And what we've done there is we're returning the data
3:25 that belongs to the value tag in that json app so we'll bring that back up and we can see we have the value there.
3:34 So what we're doing is we're getting this we're converting it to json, and then we're decoding the json I should say
3:41 and then we are getting that data there that is signed to the value key.


Talk Python's Mastodon Michael Kennedy's Mastodon