#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Converting from Flask to Quart
0:00 Are you ready to convert our Flask app to a Quart app?
0:03 Well, that actual transformation is super easy.
0:07 Well see, most of the time we have to spend on
0:09 actually rewriting our web API calls
0:13 or work consuming services with requests
0:16 but lets just convert this to Quart real quick.
0:18 So, we go over here
0:19 and we going to comment that out and put Quart.
0:23 Let's just rerun this pip install -r requirements.txt.
0:25 Download the Quart stuff. Okay, that went great.
0:30 And let's while were here go ahead and uncomment these bits.
0:34 aiohttp has a client like requests which
0:37 lets you make calls to web services
0:40 but it does it in an async friendly version.
0:44 Our dependencies are switched, of course
0:47 we're still using Flask up here.
0:49 Let me just give you the quick overview
0:50 what we're going to do.
0:51 Where you see Flask, we put the word Quart.
0:55 If you see a lower case flask, you put lower case quart.
0:57 If you see upper case Flask, you put upper case Quart.
1:00 Let's try this. I know, we can all do it, it's no big deal.
1:07 That was good. And we got to go to our other views
1:09 these should be the only other two places
1:11 where Flask appears. So we go Flask is now Quart
1:16 we're going to do a response we do it like this
1:19 we have one more place with Flask with this blue print
1:22 and then we're also using abort and jsonify. That's it.
1:29 We've converted from one web framework to the other.
1:32 We converted from Flask to Quart.
1:34 Let's run it and see what we get.
1:37 Oh, yeah. Over here we have the way this is running
1:41 it set this up as a Flask server.
1:43 Let's just run it directly.
1:46 There we go. Now, we're running on here again
1:51 let's see if it still works.
1:52 Still does. Let's see if our API still works.
1:54 Oh, 'cause it's 5,000, let's put 5,001.
1:57 There we go. 'Cause I changed that.
2:00 Okay, so our API still works.
2:03 Everything is good.
2:04 Are we getting all the benefits
2:06 the magic of asyncs that I've already shown you?
2:08 No. Let's go look.
2:11 The only part that really much matters is over here.
2:16 This method, is that method an async method?
2:21 Right there. Sun, is sun an async method?
2:23 Well, no. 'Cause it doesn't say async.
2:26 So in order to actually take advantage of Quart
2:30 we can run on it
2:31 but this stuff still behaves basically
2:33 identically to Flask. But what we have now
2:37 is Quart would be able to process this
2:40 if we wrote an async method.
2:42 Whereas Flask couldn't do anything with this.
2:44 It wouldn't work. That is all you could do.
2:47 But in Quart we can now start writing this.
2:51 So if we just put an async here
2:53 and make those async and we await them, magic.
2:57 Let's do that next.