#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Introducing our web app: The cityscape API
0:00 We have two more directories in our
0:02 GitHub repository under the Quart section.
0:04 So, we have cityscape_api and we have async's cityscape_api.
0:10 So, what is this about?
0:11 Let's open up this project and see what's goin' on.
0:14 So, again, we're going to have a lot of
0:16 requirements to install this.
0:17 We're going to create a virtual directory
0:19 virtual environment here, rather.
0:23 So, go over here and we'll just do our virtual environment
0:27 thing that we did before, and then open this in PyCharm.
0:30 In Mac OS you can drop and drop this here and it'll open.
0:33 On Windows and Linux you have to say file, open, directory.
0:38 Now, of course, to run our application
0:40 we need to install the requirements.
0:42 So, we're going to use Flask.
0:44 We're going to use Requests.
0:45 Now and later we're going to use something called aiohttp
0:49 so we can make our web service calls asynchronously.
0:52 Basically, these three replace
0:54 are the asynchronous equivalent of those.
0:59 So, we got to install Flask and all
1:01 of its friends, as well as Requests.
1:03 Okay, so let's just really quickly look
1:06 and see what this web app is about.
1:09 And I mean, mark this is as excluded
1:13 and this one as resource root.
1:15 So, if we go down here
1:16 let's just look at our app from the top.
1:18 So, we have these services.
1:19 These services we need to figure
1:21 but these are the things that call our web APIs.
1:24 I'll introduce you to a couple of services
1:26 that we're going to do, but basically we need to know
1:29 the latitude and longitude given a city.
1:33 We're going to give you the sunrise and sunset
1:35 information for the city you ask
1:37 and we're also going to give you the weather for that city.
1:40 And we need our location service to help drive that service
1:44 so we don't have to ask people for weird things
1:46 like what's your latitude and longitude.
1:48 Okay, so we're going to create our Flask app.
1:50 This is standard Flask.
1:52 We're actually decomposing it into various pieces
1:54 using what's called blueprints.
1:57 So, instead of writing all of our view code here
1:59 we're putting it into these pieces
2:00 we'll see them in just a minute
2:02 but this register blueprint let's us
2:04 sort of still hook in those routes.
2:06 We're reading out some config files that we put in here
2:10 a development and a production one
2:12 and we're going to set it to
2:15 I think, what is it set on right now?
2:16 Let's set it onto debug true.
2:21 So, it's going to set up things about how it runs.
2:24 We're also going to tell the services to use cached data.
2:27 We're going to set that to be false initially
2:30 but later we want to do some performance testing.
2:32 And it turns out that these services
2:34 cannot take the amount of traffic
2:36 or at least they refuse to take.
2:38 They may, like detect it, and say
2:39 go away, you're doing this too often.
2:42 But they have a limiting, that either limiting
2:44 or failed performance that will not let it work
2:47 well enough for our asynchronous version
2:49 when we try to push it to the max.
2:51 So, I had to set up this caching mechanism
2:53 so that we could actually truly compare
2:55 the performance of Flask versus Quart.
2:58 Okay, we'll see how that works in a minute
2:59 and then just standard, just run our Flash app
3:02 which is app.run, and this one running on port 5001.
3:06 All well and good. Let's look at our service.
3:08 Home is super boring.
3:09 It just has an error handler and it just says
3:11 welcome, use api/city for your API calls.
3:15 Really, 'cause this is an API
3:17 primarily not a website, but it could be.
3:19 All right, works, Quart and Flask just as well for websites.
3:23 Okay, so we're going to go over here and import our services
3:25 and we have two services you can call.
3:32 and that will give you weather data.
3:34 So, let's try that real quick here.
3:37 Okay, notice it running, and here we got our view.
3:40 It says that we can go to this API weather.
3:44 Let's put zip code in there
3:48 at 70002, and let's put that in US.
3:50 This'll be somewhere, Oregon, near Portland.
3:53 Goes in, makes a call. Look at this.
3:56 This is the weather right now.
3:57 Here's our longitude and latitude actually coming back.
3:59 Here's the temperature.
4:01 That seems kind of intense, right?
4:03 The max and the min for the day.
4:05 We have our wind and so on.
4:07 This actually is a little bit bizarre.
4:10 This is in Kelvin, yes, Kelvin.
4:13 So, you come over here
4:14 and that's 23.6 Celsius, or 75 degrees.
4:20 It's in the evening in the summer.
4:21 It's kind of a nice summer night
4:22 so yeah, that looks totally correct
4:24 and I guess I got Salem for the city
4:26 which is also in Oregon, but a little bit away from here.
4:29 So, here's our weather API, and we could also do
4:32 sun, I think it's just called sun.
4:35 Here we go, and we got the sun.
4:37 The sunrise is at 6:00 a.m.
4:39 The sunset is at 8:30 p.m.
4:42 Pretty much, pretty accurate, sounds like to me.
4:45 So, here we can get sun data
4:46 we can get the weather, things like this.
4:48 Of course, we could add more and more
4:49 services to it, but this is our service.
4:52 Let's really quickly see how they look and how they work.
4:56 So, let's just take the sun one, for example.
4:58 This one is the most complicated, 'cause it has to use
5:00 a location and then the sun service.
5:03 So, we go here to this location one.
5:06 It's going to come in here and create an URL
5:07 to this data science tool kit, street to coordinates.
5:12 So, this is a free API you can call
5:14 given some kind of expression for an address
5:18 and it'll return you the latitude and longitude.
5:20 Now, I've hard-coded some near my location
5:24 if we decide to use that, but right now
5:27 I turn that off, so we're using
5:29 the real data here, as you saw.
5:31 We got Salem, not this other place there.
5:34 It goes and it uses requests to download that JSON
5:39 and then it pulls that data out of the JSON document
5:42 and shoots it back to us.
5:44 So, that's the location service
5:46 and then if we look at the sun service
5:47 it's very, very similar.
5:49 Get a URL, we're using api.sunrise-sunset.org.
5:55 We're going to pass that along.
5:56 And then we can, again, use requests
5:58 get our JSON result back, and process it.
6:01 Well, this is all good, but are we getting somewhere?
6:05 Yes. We are massively, massively waiting right there
6:10 and even this part, a little bit, could be, if there's
6:12 a lot of data waiting for it to stream down.
6:14 Okay, so this just is the beginning
6:16 of the request, and this is kind of the end.
6:19 Same thing over in the location one.
6:21 We have to wait. So, if we could do other work online
6:24 while white line 21 is waiting on sunrise-sunset.org
6:29 that would be really awesome.
6:31 That would let us go do tons of stuff.
6:35 Here's our app. We're going to focus on primarily on this.
6:39 We're going to go use the service, location service
6:41 which we looked at using requests
6:43 to get the latitude and longitude, given a zip code.
6:45 And then, we're going to use the sun service
6:48 given a lat and long, to tell us when does the sun come up
6:51 when does it go down, things like that.
6:53 Then, we just return that data.
6:55 That data is what you saw right here
6:57 sunrise, and sunset, and so on.
7:00 Pretty awesome. See if I refresh it, it takes about
7:03 half a second for it to do its processing.
7:06 That's almost entirely waiting.
7:09 I think we need some async code.