RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Routes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 As you saw from looking at the views, the routes are critical this is the first interaction the user has with their web app,
0:09 they enter a url it's the routes that decide what functionality actually maps to that url.
0:15 So when you're working with Pyramid you'll see there is a main start up function that is basically the entry point into your web application.
0:23 Now, in real apps, this is probably factored better, but in this example we're just going to put the routing code right here,
0:29 what will happen is we will be given some settings that come from our configuration file, and we'll create this thing called a config,
0:35 and then we can use this config to register routes, so the first thing that we want to do is we want to register a static view
0:44 if you're just doing a purely dynamic API, you can skip this step it doesn't matter, but if you want to return anything that is
0:52 an image, css, javascript, anything like that you're going to want to set up one of these static folders and register a static route,
0:59 and by default, none of the files can actually be served off of the server that have to do with your web app, this is a good thing
1:07 you don't want them to grab like your Python source code or your configuration files with your API keys
1:12 that would be really bad, but sometimes you do want to serve static files and so you'll put them in this static folder you register here
1:18 and they have a cash age as well, so they don't keep getting redownloaded, and this cash age is in seconds by the way.
1:25 Ok, so once you have added your static view, you can add these dynamic views and in this very simple example we're gonna let you explore some albums
1:32 and if you hit the home page without going to /api/albums, you're just going to see some basically static HTML that says
1:40 welcome to the album API, here are the functions you can call, so I named this route docs, and it's just going to live at /
1:46 that just means it's just the basic, this is the default thing when you type the server. Next up, we're going to let the user get a list of albums,
1:56 so if they do a get request or I guess put, they will also get this route as far as the route matches go
2:04 they can go to /api/albums and and in this case we can return all the albums to them.
2:10 If they want an individual album, they maybe want to refer to it by name like maybe that's the way we have structured it in our url
2:16 so we've got some sort of like slug that is associated with the album so if it's like dark side of the moon
2:24 it might be all lowercase, dark-side-of-the-moon, so maybe the route we're looking for is /api/albums/dark-side-of-the-moon,
2:33 so we're going to use this little name fragment here, that will let us actually name a part of the url, and then as you saw in the previous section
2:43 we can actually pull out the value for whatever goes in there and use that in our query, so this routing matching template thing is really nice.
2:51 So you can have either one like the api/album that's just static if you will, or you can have one that's passing data to the view methods
3:00 with /api/album/{} whatever you want to call it, in this case I'm calling it name fragment, and that's how it will be passed to the view;
3:08 once you get all this set up, then you tell the system to scan all the Python files that it knows about in the project, it will scan them
3:16 and see if any of them have that view decorator, the view config, and if they do, then it will wire up that function to the name routes here.
3:24 Once you have this all up and wired together, you're ready to just start the app, so you go to the config and say here's the app,
3:32 run it, and then you're off to processing requests.

Talk Python's Mastodon Michael Kennedy's Mastodon