#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Introducing our web app: The cityscape API

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


Talk Python's Mastodon Michael Kennedy's Mastodon