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