Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Data flow from controller to template demo

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