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