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