#100DaysOfWeb in Python Transcripts
Chapter: Days 17-20: Calling APIs in Flask
Lecture: Writing the Pokemon route

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here's the routes.py file and I've created a get pokecolours function for us that's this one here.
0:09 And what it takes in is the color that the user enters. So this is an object that we are passing into this function.
0:19 And you can see straight away we have our requests dot get I've added in color dot lower here, what this is doing is whatever color the user enters
0:32 we are going to change it to all lowercase. And the reason is, with that endpoint of the Pokemon API for colors
0:40 it only takes lowercase letters, it actually matters. If you were to enter in the word black with a capital B, this would fail.
0:50 Right, so to protect us against that little tiny thing we are going to manually say that every color that is entered, every word
1:00 is going to be changed to lowercase. And that is simply going to be tacked on or appended with the little plus sign there
1:07 onto the end of our endpoint there, all right? That gets returned and assigned to the object we decode it with JSON, assign it to the poker data
1:19 as you saw in the previous video and we create our empty list of Pokemon. We then iterate through poker data
1:28 looking for the Pokemon species key, there was only one. And with all of the little mini dictionaries that were attached under this one as values
1:38 we are going to take the name key from those dictionaries and append them to this Pokemon list.
1:46 So every Pokemon in there that matches that certain color is going to be thrown into this list here. And then we return the Pokemon list.
1:54 And that's it. So now the next question is where are we going to call this function? Where in our app dot route for the Pokemon page
2:04 are we going to do this? All right, here is our Pokemon function here for the app route for the Pokemon webpage.
2:12 What we need to do is we need to actually start referencing that form that we created in our template. So we're going to say, all right
2:20 if that form sends us data then what are we going to do with it? So let's enter that in here. This may seem a little confusing
2:28 so just go with it for a second. We also need to create an empty list of those Pokemon explain why in just a sec. So we'll go if request.method
2:40 so remember, we got that method is not allowed thing, right? So if the request dot method is POST and pokecolour is in the request form
2:54 so that request form that we created we gave it a label, we gave it an id of pokecolour and we're referencing that here, we're saying
3:02 so if we are getting a POST method from our webpage and it's coming from a request form that is the id pokecolour then we're going to do something.
3:15 So if request dot method is POST and pokecolour is in request.form then what are we going to do we're going to take the color
3:29 here's request.form.get('pokecolour'). So when people entered a color we designated that color the id of pokecolour.
3:40 And we're taking that and we're going to assign it to the color object in our Python script. So this is the link between our front end and back end.
3:50 So we've just taken the data from the user and we're assigning that to the color object here. Now there is something I want to show you
3:59 and you've probably picked up on it. It's this here, I'll explain that just a second. But let's continue down.
4:06 Now what we want to do is we're going to take this list we'll throw one in here an empty list just like we did below Pokemon equals empty list.
4:17 And what we're going to do is we are going to assign that the list that is generated by our pokecolours function.
4:27 Okay, so this line is taking our function get pokecolour it's passing it the color that the user has specified.
4:36 And then it's spitting out that list of Pokemon names and assigning it to this Pokemon list here. And then we can return this list.
4:45 Now that we have this list we've built it and we have this list we can return it with our render template.
4:50 If you remember from our Chuck Norris joke thing all we really have to do is specify the name of the object we are passing to the form
4:58 and what name it's going to have in that form, all right? So we can go down here Pokemon equals Pokemon. And just to reiterate, this here
5:14 on the right of the assignment operator there is our Pokemon list here. And it's going to be called in our Flask template
5:22 it's going to be called Pokemon. Now, before we continue on, we do have to do a couple of little extra additions to our code here.
5:30 First and foremost, at the very top we need to actually allow ourselves to import the request functionality of Flask.
5:40 Let's just save everything we've done. All right, and we have here from Flask import render template. Well, now we want to import the ability
5:54 to have a request, to have a form so import requests. So from Flask import render template comma request
6:03 that will allow us to actually use that functionality. And going down, we actually need to tell our app route for Pokemon
6:14 that it has that POST method there. So let's just add it in here. Methods equals GET and POST. So we can both pull and push data from this page.
6:33 And that is going to allow us to actually work with our template. And this should work. So now all of this code is there.
6:41 We are now pulling the color from the user. We're talking to the API and then we're going to return that list of Pokemon back to our page.


Talk Python's Mastodon Michael Kennedy's Mastodon