#100DaysOfWeb in Python Transcripts
Chapter: Day 50: Responder framework
Lecture: Cleanup and refactoring for real web apps
0:00 The last thing we want to do
0:01 is just reorganize this.
0:02 Now, if this was the entire application
0:05 I'd be fine to leave it the way it is.
0:06 But I know how real web apps are.
0:08 They have hundreds or maybe thousands
0:10 of these view methods, and templates
0:12 and all sorts of things.
0:14 So you want to reorganize it and set yourself up
0:16 for growing into a bigger app than this
0:19 if that's at all possible.
0:20 So how are we going to do that?
0:21 Well, the data thing is fine, the templates you can see
0:23 I've already organized them in a way that's pretty good.
0:26 Similarly, for the static files here.
0:28 So those three are going to be fine.
0:30 What we're going to focus on is just this file.
0:32 Now the one thing we need to do is
0:34 this thing, you can see it's used here, and here, and here
0:38 and it's an instance.
0:39 So it's a little silly, but what I'm going to do
0:41 is I'm going to create just a file that basically has
0:44 that one single line in here.
0:46 So I'll come up with something like api_instance
0:49 and we'll import responder, and I'll say
0:51 API equals responder.api, like that.
0:55 And that's it.
0:56 So over here, we're not going to do this.
0:58 Instead, we're going to say from api_instance import api
1:03 and that puts us back.
1:04 But what that means is other files can also import this
1:07 and share that instance.
1:09 If I put it here, and then this file has to import
1:12 some other files, but those files need access
1:14 to that instance, so they got to get back here first
1:16 it gets really complicated.
1:17 So this kind of breaks that circular dependency.
1:20 The next thing i want to do is I want to have some views here.
1:23 Let's have a whatchacallit, like a home view
1:27 hence the name home right there.
1:29 And, woops I didn't want that to be a directory.
1:32 I want that to be a file.
1:34 Have a home set of views, and I'm going to have API views.
1:40 You could just call it API
1:41 but it's kind of highly confusing
1:43 that this is also called API, so let's call it api_views.
1:46 So then we're just going to say well, this kind of stuff
1:49 is what's going to go over in this part
1:52 and we're going to have to say from api_instance import api
1:56 and this one's done.
1:58 Now, again pretty simple, but in a real app
2:00 you have tons of these and they'll build up.
2:02 So that'll be good to have that out of the way.
2:04 And then here's our API methods, these three.
2:08 Let's take those and put them here.
2:11 And again we need the api, you can see the arrow there
2:13 from api_instance import api.
2:16 We're also going to need responder, and this thing
2:18 really was only defined here for that purpose
2:22 so I can put that up there.
2:23 We also need our db, so it looks like
2:26 we have that all up there, everything's cleaned up.
2:27 Now if I try to run this, how well is this going to work?
2:30 Well, we initialize our database and we call run.
2:33 How's the responder going to know about these routes?
2:35 Probably not very well, let's try.
2:37 Yeah, not found. Didn't know so well.
2:39 So what we need to do is basically let Python
2:43 more importantly the decorator see these
2:45 and the way you let that see it in Python
2:46 is you just import it.
2:47 So we'll say this, from views.home import *
2:52 we'll use star which is normally discouraged
2:54 but we'll use it because as we add new ones
2:56 it's just going to get everything.
2:58 And what else we want here?
2:59 api_views. So we can go say, don't do this.
3:03 Don't tell us these are not used
3:04 because they are used. Alright, great.
3:07 Now if I run it, it's not looking great.
3:08 We still got one more thing to do, but it's getting there.
3:11 Back to work.
3:12 Woo hoo, everything's working again, see that?
3:14 But now if we want to go and add something
3:16 we know where we go.
3:17 Here's more html template based views
3:20 here's where we put some more API methods.
3:22 Those are all separated from say, the app start up
3:25 like creating the global_init, importing the views
3:28 creating the app sort of over there and then calling run.
3:31 So you might want to add a little bit of extra work here
3:35 to make this, I don't know, look a little better.
3:37 We can put his into a main, or something like that
3:39 and then use our Python convention one more time.
3:45 Here we go.
3:46 Alright, so I'm going to call this much more cleaned up
3:48 and ready for big boy web application
3:50 that actually does a whole bunch of stuff
3:52 doesn't just cram everything into that app.py file
3:55 like so many of the Flask apps do.
3:57 Alright, well that's Responder all put together for you.
4:00 It's a pretty cool little web app.
4:02 One of the important things we haven't spoken about
4:04 is because it's an ASGI framework based on Starlette
4:07 we could put async here and use say like
4:11 await right here, if the database actually supported
4:15 async behaviors. It doesn't.
4:17 That's why I didn't talk very much about it.
4:18 But one of the big advantages here
4:20 which some of the others do have as well
4:22 is that we can use async on our view methods
4:24 which is pretty awesome.
4:26 Alright, that's responder in a nutshell.