Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Data flow from controller to template demo
0:01 So now that we have this data, let's go and get into our controller, pass it to our view and then do some interesting stuff with Pyramid.
0:08 So over here, we are going to this is typically how it looks in these action methods,
0:14 you map to some request, it gets you here based on the URL with data passed, you do some data service as in web service access here to get some data,
0:24 look up what user's logged in, something like this, and then you are going to return the model. And remember, the model has to be a dictionary.
0:35 Not a list, a dictionary. So let's go over here and our data access is going to be to use this services thing,
0:41 so we'll say "from...", come over here to our album service, and we'll say something like "albums = AlbumsService.get_albums".
0:52 OK, the way we wrote it it's one of these static methods, and then we are going to return a dictionary, so we'll do something
1:00 like we'll give it the name albums, is this albums. Now that might look funny, this is just a key,
1:05 it could be anything but albums seems like the right choice, I guess we could make it a little more obvious,
1:10 it doesn't have to be the same, this could be all_albums or something like that. So now this completes the data access the controller side of things
1:21 and its job is to just hand off the data to the view, into our index.pt and notice we have this albums and within the albums
1:30 each one has some..., we have a bunch of them, it's a list, each of them has title, year and tracks and then the tracks have information.
1:37 So that's what we have to work with. So let's just print them out into this area, so now we are going to get started with Chameleon,
1:44 now the first thing we want to do is we want to maybe write out the standard shape of what we want, so let's just start out let's say we have an image
1:52 and the source is going to be empty for a minute, the alt is going to be empty for a minute, line break here,
1:57 we'll put the title, so we could say H2 is going to be the "title" and then down here we'll just have a little div and this will be track count
2:08 like so and I'll say six tracks or something like that and if there is a preview,
2:12 we want to have a link, we'll just leave it empty for now, we'll say preview.
2:17 Not all of them have preview, so we only want to show this some of the time.
2:20 Let's see how this looks, it's not going to look great but we will click on albums,
2:26 here you can see we have the title, the track count and the preview, it could be worse. OK, so how do we tie this thing...
2:34 let's clean this up a little and sort of isolate this a bit. How do we tie this thing, let's give it a class of album-in-list like that,
2:44 how do we tie this thing back to the albums and how do we do a loop, things like that? So, when we get to controlling this HTML dynamically,
2:53 you'll see that there is a bunch of stuff under "tal" for template attribute language, so we have attributes, case, conditional, on error, replace,
3:01 all sorts of things and the one that we are looking for right now is repeat so in here we are going to say something like "a albums",
3:08 now that's not very self-explanatory but albums is a collection that comes to us
3:12 from the controller, and this defines a local variable each time through we'll be able to like look at a.title and things like that,
3:22 let's just say that we have two of those now, if we go and refresh. Ok, this is a good sign, one- it didn't crash, two it duplicated it,
3:29 let's try to set the title, so any time we want to work with the some property or code
3:34 and we want to then convert that to a string in the DOM, we'll use $, let me say a. to know what's in here, so a.title, it looks like that,
3:44 it can also be something computed, it doesn't have to be just straight up attribute access so this could be the length of a.tracks, something like that
3:55 and it will leave the preview alone for just a minute, let's try that, great, look at this, so now we have
3:59 Digital Age Boys and Girls, Year of the Snake, those are the two albums we have the number of tracks - five and preview and preview.
4:06 We also have things like what year is it shown and so on, let's go and put this image source here, now if we look back at our data,
4:15 we'll see that there is a URL if we were going to try to link to it, whether it has a preview and an image so first let's put the image there so again,
4:24 this maybe should be renamed "image_url", something like that but let's see what we get now, oh yes,
4:29 so not straight up image, we are in a loop, the loop variable is "a", so "a." there we go, so we have these two huge images.