Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Home page cleanup

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It looks like our web app's working but if we run it after we've imported data let's see what we get. It's not going to be as fun as you might think.
0:10 No, that's not so good. Remember we had a relationship on our releases package our releases object in SQLAlchemy that you go back to the package
0:21 and we had done a join and you know we don't have this relationship we could set up something in that style, in MongoDB
0:29 but it's probably not the best practice we don't have loading and things like that. So what we're going to do is just change how we're doing this.
0:35 So lets see what is the problem. The problem is actually down in the template here. So if we go to the home to the index
0:42 you can see down here that we're seeing for each release I want to navigate that relationship to the package and then get it's ID.
0:49 And go to the package and get a summary. So what we want to do is we actually want to just have some way to get the package.
0:57 Now there's a ton of ways we could implement this. Let me try to do it in a relatively efficient and fast way.
1:04 How do we exchange data with this template? Well of course we use our view model, so let's go over here. So we have the releases
1:12 and we could just come up with self.packages and how do we get those? Well we go to our package service
1:18 and we could say well give us all of the packages that would be super inefficient. We could say for all the releases here give us all those packages.
1:29 They come back as a list, that will work but then how will we figure out which is the right one? What we're going to do is we're going to come up with
1:35 something we're going to call a package look up. And this is going to be a dictionary that given a package_id will give us the package
1:43 and the goal would be to go with these releases and find all the packages they correspond to it. So let's go over here and say packages
1:51 and when we get a list from our package_service this is the one we have to write right now. We would get_package_by_id
1:57 and we want to get_packages_by_ids. What ID's are we going to put in there? Well let's go like this, package_ids.
2:05 It's going to be a list comprehension. So we're going to say are not package_id for r in self.releases, that's cool.
2:13 And then we're just going to pass this along. This will be a simple in query as you'll see in just a second. So let's go and have to write that.
2:20 This is going to be a list of str and what we get back, say, list of package. Now down here, this turns out to be a super simple thing.
2:31 I'm going to go ahead and use a list to make sure that we convert that all the way to a list not some kind of partial executed query.
2:36 And we go down here and say, let's see, we want packages. So we're going to Package.objects(id__in=package_ids)
2:47 That's it, we just want to get all the packages that happen to be in this set. So we're going to pass in like say
2:55 and you know who knows, some other thing past that. We want to get all the packages who's ID is one of those.
3:03 And that's it, make sure we close everything off quickly. Yeah it looks good. So if we write this function, now we can come over here
3:10 and say okay we have these packages but that's again not a clean way to look up things. That's a flat list, so it'll be slow and cumbersome.
3:16 So we're going to create a dictionary a little dictionary comprehension. We're going to say p.id:p
3:23 so given an ID we want the package for p in packages. That's it, that's all the code we have to write. It turns out it's a super fast query.
3:31 Now, how do we use this? Well we have our little lookup here. We're here, we've not talked about this but you can actually create variables in Ginga.
3:40 So we can say set package=package lookup.get R not package ID. Something like this. So we know that the package is going to be there
3:51 because we generated it right. We've gone to the releases and said give me all the packages where the ID's are this.
3:57 So now we can just go over here and say put package, package, package, like so. Let's see if that fixes it. Make sure we run it, here try again.
4:07 Tada! And did it work? Yes of course it did. There they are, so the home page is now fixed. Now the question is, is that super slow
4:15 like is this going to be a big problem going to the database twice instead of once? No, we're actually going a couple of times going to the account
4:22 going to make a couple calls here for these we're going to do, you know to load the rest of the page we're doing 2 queries.
4:30 Email literally says, and then for those releases give me all the packages. Yeah it's a little bit, but let's see what the cost is.
4:38 If we go over here to our network and we load the page a few times now there's 6 or 7 milliseconds, that's pretty awesome.
4:45 So, I'm totally happy with the speed of this page. If you're page is 6 or 7 milliseconds you should be happy, that's going to be really fast.


Talk Python's Mastodon Michael Kennedy's Mastodon