Async Techniques and Examples in Python Transcripts
Chapter: asyncio-based web frameworks
Lecture: Demo: Introducing our Flask API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about the web app we're going to build. It's already created as a Flask app. It takes a while to build
0:07 and I don't want to spend all the time doing that just diggin' into Flask. It's not a course about Flask per se.
0:13 But we're going to take a Flask app and we're going to make it asynchronous and we're going to implement all of the things you have to do to go
0:19 from the traditional synchronous style to asynchronous which is going to be pretty awesome. So I want to introduce you to it and then
0:27 we'll start programming the async transformation it's going to go through. So there's actually two web apps here Cityscape API and ACityscape API.
0:36 Right now these are both the original serial version. This one we're going to transform into the asynchronous version.
0:45 So I'm going to leave that there for you to have as a record. This is the one we're going to work on. So let's just go through really quick.
0:51 Now I don't assume that you know Flask like I said but we're not going to go into too much of the details.
0:57 You'll see enough that you'll be able to take it away and if you learn a little bit about Flask then you'll be able to adapt this, no problem.
1:03 Maybe the best way to introduce you to it is to actually see it running so let's start it real quick, open it up.
1:09 And it says we can use this API, sun/zipcode/country is one of the APIs so let's put that in here. So zip code, might be 97227 if you could type the 9.
1:23 And the country code would be something like US or your country and you can put your postal code in there. And you hit this, and what we get
1:31 see it took a second 'cause it really went out to the Internet to another service a couple services actually and it figured out what sunrise and sunset
1:39 in and around Oregon is. I don't know exactly where that is, but which where this zipcode is but it's pretty close to me and this looks about right.
1:47 So sunrise at 6:48 a.m., sunset 7:30 p.m. We can also ask for the weather. It'll give us all sorts of details back about the weather
1:56 how much cloudiness there is. What's the humidity and pressure. The moderate rain apparently is something happening right there so, pretty cool.
2:06 So we can basically ask for sunrise, sunset and we can ask for weather. That's what this API does. Now let's see how it goes about doing it.
2:16 So this, this runs when everything starts up. This creates our Flask app so here's our Flask app. And we've used this concept called a blueprint
2:26 a Flask blueprint, to take the sort of HTML view stuff and put it in one file and the API bits and put it to another file.
2:35 So it let's you separate our apps. I really like that pattern. And then it just goes and it sets some values out of config files that live over here.
2:44 Then it calls run. So that's pretty straightforward. Let's just look at the API bit over here. So for example let's look at the sun
2:51 it's more complicated. So you give in a zipcode and the country as strings and we have to call this function
2:57 to actually get the latitude and longitude. That is actually what's required for the sun_service that we use say given any spot on the globe
3:05 what is the sunrise, sunset information? So these services right here this one, and this one those are implemented over in the service section
3:17 so we have the location_service for example. Sun_service for example. So I'm going to hide away this cached data 'cause we're not using it right now.
3:26 This is only if you want to do performance testing we'll talk about that later. So we're going to go in and we're going to use requests
3:32 we're going to go and just get a call and we're just going to do some json parsing here. And then we have to convert from UTC to local time
3:39 just so things make sense to you in that location when you look at it okay? So this is it. How are we going to add asynchronous into this?
3:50 Well this function right here could be async theoretically, except for well see, Flask does not support any async capabilities whatsoever, at all.
4:04 We'll talk more about that in a moment. There's actually an interesting piece of the documentation and some interesting news around that as well
4:10 and depending when you watch this that may or may not be still true but Fall 2018 Flask definitely does not support
4:17 any form of async. So we're going to somehow find a way to make this function async and then we'll be able to implement that in aiohttp client style
4:26 as we did before, same for this. And then we can await those and instead of having this method just block while we're talkin' to that service
4:32 block while we're talkin' to that service both of those times, when they're blocked will let other requests come in
4:39 and maybe process the weather request. Or a different send request, things like that. This is our app that we're going to start working with
4:45 and this is the app we're going to use for our demo. Like I said this one is hangin' around for you to just have as a record.
4:52 This one right now is synchronous but it's going to be converted to an async enabled, Flask-like, Flask-based API but it won't actually be a Flask.

Talk Python's Mastodon Michael Kennedy's Mastodon