Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Pyramid building blocks

Login or purchase this course to watch this video and the rest of the course contents.
0:02 It's time to take a quick tour of the various building blocks or the concepts that we use in Pyramid to build our web applications.
0:11 But what are these building blocks? Everything starts with a route. When we get a request into the web server,
0:18 we're only given a URL and the URL has to be mapped over to some sort of behavior. And Pyramid is a MVC, model-view-controller framework,
0:29 which means we need to come in and figure out which function should that map to which controller. And then let it process that and return
0:38 whatever view it decides makes sense, okay. So the first thing that we're going to do is to define a set of routes or pattern-matching on URLs to
0:47 figure out where does that request get handled within our application. Then we'll have, what Pyramid calls views, but I'd prefer
0:54 to think of as controllers because of the MVC nature. And these are either methods or they can be classes that process our requests.
1:02 In our course, we're going to stick to the method style of working, but think of it as just something that you can
1:08 call some function or method that can be called to actually handle the request. Here's the URL. Here's the data associated with it.
1:16 Maybe from the URL itself. Maybe from a query string. Maybe from a POST. Take that data and just process the request whatever that means to you.
1:25 Once the request has been processed, we need to generate a response. And, very often, what this is going to be is some form of dynamic HTML.
1:33 Maybe you run a podcast and you want to be able to say /<podcast_number> is going to show the details for that podcast.
1:40 Well, the template itself, the basic HTML stucture's always going to be the same, but the various pieces of data,
1:48 what is the description, what is the play link and things like that, is going to change. So we want some kind of dynamic HTML.
1:54 See the Pyramid has at least three options on how you can build these. Three different templating languages you can use. But really nice support there.
2:04 The data that is passed from the controller down to the template, this is called a model. So this is both data and behavior passed to the view.
2:14 And this is typically done in the form of a Python dictionary. There's also support for static, or cached, assets.
2:21 So if you've got CSS, JavaScript images, those types of things, one of the easiest things you can do to make
2:27 your website seem ultra-fast is to cache those really, really carefully. So you'll see that maybe, even though you might return,
2:35 I don't know, 250K of JavaScript and images on a particular page, if your browser caches that, that site
2:43 is going to get much, much faster after the first request. Of course, configuration is super important. The way we want our app to work locally,
2:51 whenever we're working on it. The way we want it to work in production. These are probably very different things. Maybe different database connections.
2:59 Maybe one has outbound email turned on for all the actions. Like if you click the reset password button for a user,
3:06 well that's probably the user doing that in production, they want to get an email. In development, you want to make sure that does not happen.
3:13 If you're testing some problem, like I'm interacting with a user that says, "I can't get into my password reset,"
3:18 and you touch that button, logged in as them, you don't want to actually send them a bunch of fake emails.
3:23 So you want different configuration settings. No email in dev. Real email in production. Things like that. So Pyramid has support for that, as well.


Talk Python's Mastodon Michael Kennedy's Mastodon