Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Refactoring view methods with Flask blueprints

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So here we are in our Flask app. And it's pretty standard, right? This is like how they tell you how to do it in Flask.
0:07 Import flask, you create your app and then you create your view methods. You put your app.route on them.
0:13 Well, okay, that's how you get started maybe sometimes. But this is a terrible way to organize real applications.
0:21 It definitely encourages just cramming your entire web app into like a single file. Bad idea. And it's bad because Flask has a really
0:30 super cool way of organizing your views that you don't have to do much at all about. You just change basically a one word right there
0:38 and you're good to go. So what we're going to do in this little short segment is we're going to reorganize our code not because it's really that long
0:46 you can see it's actually short but so that it's ready to grow as we build up this real application. So let's do a couple of things.
0:54 The first thing I want to do is actually deal with this fake data. We're going to have a real database with lots of interesting stuff going on.
1:00 So I want to create a special part of my application just for that. I'm going to call that services. Not web services, but services that are provided
1:10 through our application like stuff that deals with packages stuff that deals with users, and so on so we can isolate that over here.
1:17 So I'm going to create something called package_service. Like so. And then let's just take this code and put it over here.
1:28 All right. Not super interesting. Remember, we don't have our data yet. We don't have our database. That's coming later.
1:35 But, we can get started this way. So we're going to come here and say import and let's say as just package_service.
1:45 I noticed there were some errors down here so we can come and say .get_latest_packages. Just like that, I think that's the only place it's used.
1:52 Okay, let's just make sure real quick that we didn't break it. We did not, okay super. That's what I expected. So, that organizes it a little bit
2:03 gets some of the data access out of here. But this, this is the ultimate problem right there. However, it's tricky to reorganize it as is.
2:12 So this needs to be created and then it needs to be used to start the app in this main And yet, it has to be available at the time
2:21 that these are parsed so that we can apply the route. So, that can make it really tricky. There are patterns that allow you
2:27 to share this across the file, different files but because Flask has this built in thing called Blueprints which are ways to predefine
2:36 these routes and then later apply them after the app is created, we're going to use that. It's really nice.
2:42 So, let's go down here and create a folder called views. And in the views folder, we're going to create let's start with two things
2:50 we'll have a file called home_views keep adding that to get up. And we'll create another one called package_views.
3:00 We don't really directly work with these often so the name isn't super important. Okay, so what we need to do is we want to take
3:08 this is going to be the home stuff and we're going to put it over here. Well, immediately you can see the problem.
3:15 Some of it's just an import problem, that's easy. Let's go up here and grab that 'cause of my renaming.
3:24 Okay that was easy, but the one that's left here hmm not so much. So, we're going to use this thing called a Flask Blueprint.
3:31 And the way it works is we're going to come over here and we're going to say Blueprint you can also call it app if you want
3:36 but let's not confuse things here. So we say Flask, which we have to import at the top we say Blueprint and in here
3:43 we're going to give it three pieces of information. A friendly name, so in this case I'm going to call it home, 'cause it has to do
3:49 with the home_views. And then for the import name I'm going to give it the __name__ which is the name of this file, home_views.
3:58 And then we want to set the template folder to templates. And then all we have to do is go down here instead of doing @app.route
4:07 we say @blueprint.route. So, we just replace app with blueprint like so. Now, this sort of works this will sort of add some features here.
4:22 And we go and run it let's see what's going to happen. We don't need this anymore. So, let's see if we run it is it going to be amazing?
4:29 The chances are it's not going to be amazing. That didn't work. But remember, potentially these these are created before the app is there.
4:39 And how does the app instance know about it? Well, we can do, there's a couple of things we can do.
4:44 But, let's define, let's do a little bit of work here. Let's define a main and move that up. Now, before we do this let's call another function
4:54 called register_blueprints or whatever we want to call it. Like so and then all we got to do is say so import home_views
5:08 and then we can go over to the app this is the one that now exists here and we can say register_blueprint, like so and pass it in home_views.
5:18 And that's it, that basically goes back and tells the app about the few routes and methods and what not are defined over here.
5:26 It should work, let's give it a try. And it did not like that. Oh we can't aim for home_views, we have to aim for, register home_views not blueprint.
5:39 Let's try that again. All right, there we go. So, looks like it's running but does it work? Moment of truth. Of course it works. Of course it works.
5:50 Isn't that nice? So that lets us break our code apart. Our view code into these more specialized pieces. So, it might not be convincing to you yet
6:00 how amazing this is, so let's go and add another one here for the package. So, for packages we're going to have a couple of things
6:09 and I'm just going to copy a few bits here to start out. So, we're going to have a blueprint and it's going to be packages, that all looks good.
6:17 And this is going to be packages what we want to have is maybe a details. And we're not going to do anything super interesting yet
6:28 let's just return package details for some package. And here do we get to define our first interesting route, I guess I would say.
6:40 This is going to be, let's remind us what that's going to look like., the real thing click on this one, it's going to be
6:49 /project/<package_name>, okay. So that's what we want there so here we say package, let's do package_name.
6:59 Like so. Now, notice start away that PyCharm is saying whoa, whoa, whoa there's something wrong with this part of your route. What is wrong?
7:09 A function index does not have a parameter to take it. That's so cool. So, we just go in here and say it takes a string, now it's happy
7:16 and we can actually put that there as well. Okay. Let's omit that out for a moment. So, we should be able to run this.
7:26 Let's change the name, let's call this package_details. And the blueprint, we're also going to have to register this blueprint for it to work.
7:35 So, last thing, I need to go down here to import package_views like so and then we can import it make sure everything's nice and cleaned up.
7:47 Did it rerun? Probably it did rerun. Make sure, there we go. So, we've got our two things here but if we go to /project/abc
7:57 boom, package details for abc. Okay, so we've defined our first interesting route but maybe even more importantly for the moment is
8:06 we found a way to take our single super mega monolith file that was defining our application imagine there's fifty of these methods
8:16 and breaking them apart by category. So, here we have home, these are the things to do with like home and about.
8:22 And we'll have package and we'll have some other package things going on here as well. Of course we'll use that again in just a minute.
8:29 And then in our templates folder I told you I was organize these with this in mind so here we have home
8:34 we're also going to have a new folder called packages in which case we're going to put our details.html. So, let's just rob this one and call it
8:45 details.html, of course, it doesn't have all the details yet, but it's going to. So, this organization here is directly tied
8:55 to this organization that we did with blueprints there so, I think this is a great way to organize your code nothing about Flask encourages you
9:03 or suggests that you should do these types of things, it's just a recommendation from me you don't have to but I think it's going to be
9:10 really, really great as your app grows.

Talk Python's Mastodon Michael Kennedy's Mastodon