Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Part 1
Lecture: Building Block: Routes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Here we are in the __init__ file for the entire website. Remember your website is a package,
0:07 this is basically the entry point into your package, your website. We have a "main" method here, that's going to be run by Pyramid
0:14 and the first thing it does is get a config and we are going to use this config to define various routes.
0:20 We'll begin by mapping static folder, and setting some cache details on it, the static folder allows you to drop any file into there
0:27 and it will be served up without going through the Python layer, so we can put our JavaScript, our CSS, our images and things like that in there.
0:36 Notice also that it's setting the max age and this is in seconds so 3600 seconds is an hour; in a real app you probably want to set this to days,
0:44 weeks, months, maybe even years, depending on what you are trying to do. Then we get to the more dynamic content and these are called routes,
0:51 so what we are going to do is add some routes, we'll add four here, the first one called home goes to just forward slash,
0:59 and notice, there is a name and then there is a URL pattern, so the name has to be unique, among all the other routes,
1:06 and then the URL pattern can be just a simple static URL or it can be data-driven.
1:11 The first two, home and albums, these are always the same, these are just static, the album one, however, is more interesting.
1:17 Notice it's "/albums/" some "{name_fragment}", so this is like some kind of name or title that we can use to look up the album by title.
1:27 So we could have just "/albums_1", "/albums_2" but that's not good for SEO and it's not very cool looking in the URL bar either,
1:36 We're going to make sure they have "/albums/...something" involving the actual name of the album. So when we get to our view method,
1:48 {name_fragment} is actually going to be a variable or value passed over to our view. Similarly, the store lets you buy an album by name.
1:58 Once we have all those things in place, we'll say "scan()", that will tell Pyramid to go through all of our files,
2:03 look for these view methods that have the right decorator and the names of the routes, and then wire them together.
2:10 Finally, what we'll do is we'll take this config and we'll say make a WSGI app, so WSGI, W-S-G-I is the standard way
2:17 in which all the different Python frameworks speak to the underline web server.
2:21 So this is basically saying once we've configured everything, create the app and hand it off to the web server to go run.
2:27 Now, this is a little bit tedious in the style and there is a couple of ways to reduce the TDM with traversal and various other things,
2:34 and we are going to talk about the thing called pyramid handlers that makes this much easier to sort of create a standard style class-driven URLs
2:43 so you'll say "here is a class I want to register as having views" and then it will be able to discover all the various views on it automatically.
2:51 So as you add methods, they can automatically be rendered and you don't have to come back here for every single method.


Talk Python's Mastodon Michael Kennedy's Mastodon