|
|
|
18:39 |
|
|
transcript
|
0:50 |
Hello and welcome to Data Driven Web Apps in Pyramid. In this course, we are going to take the Pyramid web framework which is a Python-based web framework and we're going to build some amazing data driven web applications. We're going to use Bootstrap which is one of the most popular CSS front end design frameworks to make our web app we build in Python look great. Of course, almost all web applications access databases. At least the dynamic ones do. And we're going to use the most popular and powerful Python-based ORM called SQLAlchemy to write Python classes and map those to our database. Welcome, welcome, welcome to Data Driven Web Apps in Pyramid. We're going to have a great time building some real world applications and learning some real world ways to put them into production.
|
|
|
transcript
|
1:46 |
Before we dig into all the code that we're going to write and the technologies we're going to use and the types of things we can even build let's just take a moment and admire the incredible power of the web. The web is one of the most flexible and certainly the most widely deployed and used technology out there. Think about YouTube. YouTube is an amazing web application. It really has brought video sharing to the entire world. It also happens to be a Python-based web app and handles millions of requests per second. Think about Airbnb. With this web application, and a unique sharing philosophy they've literally transformed the way people travel and stay in cities. Netflix uses their web app to redefine TV for many, many people. In fact, this web app and their infrastructure account for 35% of the bandwidth of the United States in the evening. That's pretty incredible. Hey, let's throw one more in. You're taking an amazing course and you're going to be learning all about building web apps. Here's another one, Talk Python Training. This site is built, of course, on Python and Pyramid and has taught a ton of people about Python. In fact, many of the techniques and the lessons learned in building this site along with some of the others I'm going to bring into this course. So it's not just about looking out there and seeing some incredible web apps built it's also taking a pretty amazing web app that I have built and taking the lessons and techniques from that and sharing them with you with Pyramid SQLAlchemy, and so on. Do you have an idea? Something you want to build for everyone? Well, the techniques and technology you're about to learn will let you unleash this incredible power of the web to bring your idea to life.
|
|
|
transcript
|
3:08 |
In this course, you're going to learn what's sometimes referred to as full-stack web development. Now, we don't spend that much time on the JavaScript side of things but everything else we do spend a fair amount of time on. So, what the heck is full-stack anyway? Let's look at all the pieces involved in a typical web application interaction. We have a browser we have our server running out on some cloud hosting we have usually a virtual machine or set of virtual machines in the background and our database we're working with. So, a request is going to come in. It's going to magically find it's way through the internet to our server. Our server is going to do some processing talk to the database, and so on. Where's the full-stack part come in? Well, let's look at the technologies involved here. On the server side there are a bunch of different technologies you need to learn. We're going to learn Python. Well, we're going to use Python at least and we'll probably learn little bits of it along the way but we're going to focus on using Python along with Pyramid the web framework that we're going to work with. The dynamic HTML templates in this case Chameleon. The data access layer SQLAlchemy. In order to deploy and configure our servers we're going to need to know a little bit of Linux. Learn that. We're going to use Nginx and uWSGI. These are two cooperating web servers that we're going to use to run our Python code in Pyramid. Then we're going to talk to the database. Well, what do you need to know for that? Well, maybe this is Postgres or MongoDB. For the course, we're actually going to use SQLite and also a little bit of touch of MongoDB at the end but it's easier to switch to something like Postgres. You may need to know the query language to get to that like SQL, or MongoDB's query syntax it's like a JSON type thing. We're not going to actually focus much on the query syntax because we're going to use SQLAlchemy which lets us sort of abstract that away but you would more or less need to know that in practice. You also need to know how to migrate or manage your database over time as it's structure changes. Look at all these technologies just on the server side. That's a lot to learn and this whole stack of technologies put together is part of what's called full-stack but there's more to it. Over here on the browser side we have HTML and CSS we have to deliver to the browser, right? That's ultimately the goal is to show stuff on the screen for the web browser. HTML and CSS, we're going to do some work with that. You typically don't want to do the design from scratch so you'll pick a front-end framework like Bootstrap. Maybe you're even writing some rich JavaScript apps with say, Vue.js. Those are grayed out, because like I said we're not doing much with JavaScript but these are the technologies that you might put into play for a full-stack web application. The web can be a little bit daunting. We're going to go through most of these technologies. All of the white highlighted ones in this course and you'll have concrete small examples for each one. In the end, we'll have it all put together in a full-stack Python-based web app using Pyramid SQLAlchemy, and Bootstrap.
|
|
|
transcript
|
1:32 |
This is not an introduction to Python course. We do assume a little bit of working knowledge. We assume that you have basic Python programming skills. I would say if you're, what you might consider an advanced beginner, you're totally good to take this course. We're going to use things like classes and decorators and functions and so on but we're not going to describe what a class is or describe what a function is or things like that. So make sure you have some basic working knowledge of Python in order to take this course. If you don't, consider taking my Python Jumpstart by Building 10 Apps course. That's all about learning Python. So here we assume that you know Python. We also assume that you know a little bit of HTML. That if you look at HTML, at least you can make sense of it you know what, say, an attribute is what a class is, things like that. We're not going to be doing too much advanced stuff with HTML. I'm not sure if there is such thing as advanced HTML but we're not going to be doing too much with it but of course, this is a web class and we're going to be doing a lot of HTML but I assume that you can work with standard markup. Similarly, we're going to be using CSS and I'll talk a lot about what the selectors and classes and design mean, and especially when we get to the Bootstrap section and applying that to our website. So we're not going to assume you know tons of it but we're not starting from scratch with CSS either. So basic HTML, CSS, and Python knowledge that's what we assume you're starting with and then we're going to build our full-stack web app from there.
|
|
|
transcript
|
3:44 |
When we talked about the incredible power of the web, we saw some amazing web apps built. Only some of them were built with Python. Let's look at a whole bunch of other apps web apps, built with Python. I think it's always great to have some examples. You know, maybe you're trying to convince your team or your boss, or something like that. "Hey, we should build this particular app we're trying to launch in Python and here are five examples of other apps that are similar, that are amazing." So let's go through a quick list to give you some ammunition. You might be familiar with The Onion. This is a fake news comedy site that's hilarious. They post all sorts of crazy, outrageous news items and that, of course, is built on Python. Spotify, the music streaming service, their web app, Python. NASA and JPL make heavy use of Python for all their awesome space stuff. Bitly, the URL shortening site that gets tons of traffic does all sorts of analytics and stuff around the URLs they shorten, Python. Bitbucket, SurveyMonkey, Quora what I think one of the very best online Q&A forums for thoughtful, somewhat deep answers. Not always great but pretty good, built with Python. They're actually pretty enthusiastic users of Python. If you look at their engineering blog they often write about how they use and configure and optimize Python for what they're doing. Disqus the comment section that you can embed into any web application. If you go to either of my podcast sites, talkpython.fm or Pythonbytes.fm, you'll see at the bottom this little Disqus section. That is built with Python. Many of their services are based on Python. Instagram, an incredible amount of traffic goes to Instagram, and they make heavy use and, again are very passionate users of Python and their engineering blog covers a lot of cool things like how they disabled garbage collection, for example to make their web apps run much faster. Reddit, the front page of the internet, as some say gets tons of traffic, I think it's in the top ten websites on the internet built with Python and SQLAlchemy. Youtube, we already talked about that. Youtube is amazing, they get millions of requests per second and are based on Python. The brand new relaunched PyPI, the Python Package Index at pypi.org, is built with Python and Pyramid. They get tons and tons of traffic and they actually tried Flask, they tried Django and they tried Pyramid and they decided Pyramid was the best choice for what they were building. Pinterest, also built on Python. Paypal has some of their pricing services written in Python, and these pricing services are called by different parts of Paypal's infrastructure and other services and websites to figure out what the exchange rate is, what the fee is that goes along with various transactions and so on. That has two to three billion requests per day written in Python with sub-millisecond maybe just single millisecond response time. It's pretty incredible. Dropbox is a heavy user of Python, the client you get on your machine, as well as much of their backing services, Python. In fact, Guido van Rossum, the guy who created Python, works there at least at the time of this recording so you can bet that it's a real center of the universe around Python. And, last but not least, Talk Python. All the Talk Python stuff, the training site the podcast site, all that stuff is based on Pyramid and Python. It's been working out amazing. We get incredible, high-performance web apps that are easy to maintain. We'll share a lot of the lessons from that experience in this course. Want to read more about all of these how these companies are using Python? Well, how about we use a little Python to get there. Bitly/pyapp-25, that'll take you over to an article where I pulled a lot of this stuff together with more background info than I put in here.
|
|
|
transcript
|
4:50 |
In case you haven't already scanned the table of contents to figure out what we're going to cover let's really quickly go through some of the topics that we're going to cover in this course. After this introduction section we'll briefly cover what you need to set up your machine and the tools and editors you need to follow along. We're going to introduce the Pyramid web framework some of its design principles and compare and contrast it with other popular frameworks. We'll create our first site. Here's where we start writing code in this Chapter 4. We're going to create our first site and get it up and running in Pyramid. We'll look at the various options for HTML templates and then, we'll dig deeper into the Chameleon template language. If you want to use another one, that will be totally fine but I make a case for why I think this is the best HTML template language out there. We'll see that mapping URLs to methods in our web app is a central part of the MVC framework, the Model-View-Controller framework that is in Pyramid and popular among many of the Python web frameworks. So, we'll focus on taking a URL and getting it to run a function and getting some kind of response. Once we have some basic pages up and running we'll look at how we make our pages beautiful. We'll bring in Bootstrap. Also talk a little bit about some other front-end frameworks if you want to use something other than Bootstrap but we'll look at Bootstrap and how to bring that into our web app. At this point, we'll have basic web app functionality happening but it'll all be based on fake, in-memory data. Of course, we don't fake, in-memory data. We want live, full-stack web apps. So, we're going to move down a little bit in that stack towards the data access layer. We'll talk about SQLAlchemy, how the ORM works we'll define classes that map our data into our database, and, of course run queries to get that data back. See, the SQLAlchemy ORM is great for creating the initial database structure but if it changes, things are going to go crazy. Right, our database is going to be out of sync with our ORM classes and you'll see that's a full-on crash. It's really, really bad. It's kind of hard to maintain the stuff. Well, Alembic, a database migration framework that's peered with SQLAlchemy will come to the rescue. We can point Alembic at our SQLAlchemy classes and our database, and go, alright. We changed these. Make the database scripts that are needed to do that change, to apply those changes automatically. Just make that for us and then we can apply that to like say, staging or production as we roll out new versions. It's going to be great. Once we have our database working we'll want to accept user input. Probably save it to the database, as well. So, we'll talk about HTML forms letting users create accounts register, enter data, that sort of thing. If you're accepting data, you had better be validating it, right? The internet is a harsh place and lots of people try to send invalid data either by mistake or maliciously. We're going to talk about some really awesome design patterns called view models for server-side validation and data exchange and then, we'll also talk about leveraging some HTML5 features so we don't have to write any JavaScript for client-side validation. Testing web apps can be tricky because web applications depend upon things like databases, and other services and the web framework, itself, right? Like the request-response model, all that. So, we'll see that Pyramid provides special infrastructure to make it testable so that we can test our web application more easily without actually creating the servers and doing heavy weight stuff like that. Once our app's built, tested, everything's working, it's time to ship it, right? Put it online, so we're going to take our web app and deploy it on a brand new Linux server out on the internet and we'll have our web app running and live. You'll see everything you need to do to take a bare Ubuntu image, and get it up and running to be our web and database server. Finally, we're going to take a look back at how we built our application how we've structured it so that we have a lot of flexibility using various design patterns for accessing data and validating data, and so on. And as an exercise, we're going to convert from a relational database to a MongoDB database. That's interesting, if you want to know about MongoDB, you'll see how to do that but it's also interesting just to show how flexible these design patterns are because we're going to make this conversion by just changing a few handful of files even though there'll be many, many aspects of our site. We'll have that really isolated and focused so it's super easy to do whatever we want with our data access. If you wanted to, say, convert it to calling web services and some kind of micro service architecture you'd see the techniques we're using here for MongoDB would equally apply to that exact same transformation. This is what we're going to cover. I hope you find it super interesting. It's a real world version of what you need to build and ship web applications. This is almost everything you need to know to go from I know a little bit of Python and HTML to running these full-stack web apps that are things like the training site or YouTube, or stuff like that. I hope you're excited to learn it. I'm definitely excited to share it with you.
|
|
|
transcript
|
0:44 |
Now, before we get to the rest of the course, let me just quickly introduce myself. Hey, I'm Michael, feel free to reach out to me on Twitter where I'm @mkennedy. I've done a number of things in the Python space, one is create the Talk Python To Me podcast, where I've interviewed a lot of people who were involved in these various technologies, Mike Bayer from SQLAlchemy, Chris McDonough, the founder of Pyramid. I'll talk a little bit about some of those episodes when we get to the right place and how you can learn more. But I started and run the Talk Python To Me podcast. I also run the Python Bytes podcast which is sort of a news one. Check that out. And of course, I'm the founder and one of the main authors at Talk Python Training. Nice to meet you, I'm really excited you're in my course now let's get started.
|
|
|
transcript
|
2:05 |
Welcome to your course. I want to take just a quick moment to take you on a tour of the video player and all of its features so that you get the most out of this entire course and all the courses you take with us. So you'll start at your course page, of course, and you can see that it grays out and collapses the work that you've already done. So let's go to the next video here. It opens up this separate player and you can see it as standard video player stuff. You can pause or play. You can actually skip back a few seconds or skip forward a few more. you can jump to the next or previous lecture, things like that. It shows you which chapter and which lecture topic you're learning right now. And it has other cool stuff like take me to the course page, show me the full transcript dialogue for this lecture. Take me to the GitHub repo where the source code for this course lives and even do full text search. And when we have transcripts, that's searching every spoken word in the entire video, not just titles and descriptions and things like that. We also have some social media stuff up there as well. For those of you who have a hard time hearing or don't speak English as your first language, we have subtitles from the transcripts. So if you turn on subtitles right here, you'll be able to follow along as the words are spoken on the screen. I know that can be a big help to some of you. Just because this is a web app doesn't mean you can't use your keyboard. You want to pause and play? Use your spacebar to toggle that. You want to skip ahead or backwards? Left arrow, right arrow. Next lecture, shift left, shift right. Want to toggle subtitles? Just hit S. And if you're wondering what all the hotkeys are, you can click this little thing right here. It'll bring up a dialogue with all the hotkey options. Finally, you may be watching this on a tablet or even a phone, hopefully a big phone, but you might be watching this on some sort of touchscreen device. If that's true, you're probably holding it with your thumb. So you can click right here to seek back 10 seconds, right there to seek ahead 30, and of course, click in the middle to toggle play or pause. Now on iOS, because the way iOS works, they don't let you auto start playing videos. So you may have to click right in the middle here to start each lecture on iOS. That's the player. Now go enjoy that course.
|
|
|
|
7:06 |
|
|
transcript
|
2:31 |
I'm sure you're ready to get in and start learning about Pyramid and building the web apps right away but before we do, let's just take a quick moment and make sure you have everything you need to follow along and write and run these programs. Obviously, you're going to need Python, right? This is a Python course and in fact we're going to use Python 3. Python 2 will go unsupported in less than two years from now so of course we're using Python 3. However, Python 3 doesn't come on all the systems. Let's see if you have Python. How do you know whether you have Python? Well, you can go to your terminal on a Mac or on Linux and type Python3 -V and it'll tell you either no there is no Python 3 or it'll tell you what version. When I ran this command it was 3.6 but actually 3.7 just came out so Python 3.7 is the latest. As long as you have 3.5 or later, you'll be fine. If you're on Windows this doesn't work. It's a little frustrating but Python3 is not a command that comes installed with Python by default. How do you know what you have on Windows? Well, you could type Python -V and that'll tell you what one of your installations is if it's in the path. It might be better to ask where Python and it will show you all the different locations and then you can type Python -V and see which one. For example, on this Windows 10 machine we have the Anaconda 3 Python which is Python 3 from the Anaconda distribution. We have Python 3.6 from Python.org, and 3.5 from Python.org all in these random, different locations here. So you can adjust your path to target the one that you want but make sure when you type Python -V you get something Python 3.5 or higher. Depending on your OS if you type this and everything works you're ready to take this class. You have Python set up. But what if you don't? Maybe you need Python, maybe that didn't work. There are actually a whole bunch of tradeoffs and different ways to install Python. One way, if you're on Mac or Windows, is just go Python.org download the latest installer, and get it. But I want to give you a little more guidance. Drop over at realPython.com/installing-Python and have a look there. They have for each of the operating systems a couple of different ways. For example, for macOS, you might use Homebrew to install and update the latest version of Python. Things like that. So go over here, see what they say for your OS. They're going to keep maintaining this and updating it over time. So this is probably the best resource for installing Python if you need to do so. Once you get Python set up you'll be ready to move on.
|
|
|
transcript
|
1:25 |
We'll be doing a ton of live demos and almost every bit of code and concept covered will be done in some form of a live demo. That means we need a really solid editor. We're going to use PyCharm. I'm going to use PyCharm for this course. And you, if you want to follow along exactly should also use PyCharm. It's in my opinion the best editor at the moment for Python. If you don't want to use PyCharm for whatever reason you can use whatever editor you'd like. If you want another recommendation one that's free, the Visual Studio Code with the Python plugin is looking like really the second best option these days. And it's really good, and it's really coming along. One other thing about PyCharm they have a community free edition and they have a pro paid edition. Often, the community free edition is totally good and you can use it for writing all kinds of stuff in Python. However, one of the paid features is web and database support. This is a class about web and database programming so as you might expect, you're going to have to have the professional edition of PyCharm to make full use of it with this course. If you don't have or want to get the professional edition of PyCharm you can follow along with VS Code. You can sort of follow along with the community edition with a few little hacks to make it run. But you won't get the full editor to support in PyCharm community. Be sure to get the right version to get the most out of this course.
|
|
|
transcript
|
1:15 |
Everything you see me write. Every bit of code that we create and all the stuff that we generate during this course and even some of the raw data needed to generate that is available for you on GitHub. So I encourage you to drop over here at github.com/talkPython/ here's a long one data-driven-web-apps-with-pyramid-and-sqlalchemy separated by dashes. Go over there and star and fork this repository so you can get back to it and fork it, of course so you have a permanent copy in your GitHub account. If you don't want to, you can just go to the clone and download button and download the zip. But I recommend that you link it to your GitHub account. That way you have access to it forever. So if you're wondering how something worked drop over here in the GitHub repository and check it out. There's a section that has the various versions the various chapters. Every single chapter we have a separate saved copy of what we built. You can go and see exactly what was built. There's a before and after section. So that you can compare and see what was done during just that chapter. Alright, so, be sure to grab this. And, and get it yourself. We're ready to go. You could start from scratch and follow along. But like I said, at some point, you'll need, like the data and stuff to generate, say, the database.
|
|
|
transcript
|
1:55 |
Now, I've tried to structure this course so it's easy for you to follow along. It's great to have a bunch of building blocks. Here's how you do routing. Here's how you do templates. Here's how you talk to the database. And then say well go forth and you can put your building blocks together. Good luck. But it's way more fun to build them up and have something you've created over time. So, I've tried to design this course in a way that you can do that and if you get off track you can sort of reset. I talked previously in the other video about having a before and after code for each section in the GitHub repository. Let me show you that right now. Here we are in the GitHub repository. If you go over here to the source section you'll see it doesn't sort it really great numerically but for each chapter that we've written code we don't actually write code until Chapter 4 but for each one we have a separate section. You'll see that say Chapter 5 is based on Chapter 4 code. Chapter 6 is based on Chapter 5 code. Things like that. So if you want to follow along with the routing part you come over here and you can see we have this start section. This is the code that we typed in that I typed in when we started Chapter 6. This is the result of all the work we did during Chapter 6. So if you want to say start from where we were in Chapter 6 and then do the routing work yourself just make a copy of this folder and start going. Of course, this will become the starter code for Chapter 7 and so on. We talked about in the course how to get one of these loaded up and running. How do you create the virtual environments and register the packages and stuff like that that you need? So, we'll get to that in due course but just know that we have these sort of before and after codes for each chapter and ideas that hopefully that helps you follow along if you get lost or something along the way just grab the starter code from a particular section or you can go back and sort of diff the two directories and see what changed.
|
|
|
|
22:25 |
|
|
transcript
|
3:41 |
Now this is obviously a course on Pyramid, so we're going to choose Pyramid as our web framework for this course, but let's talk through the various considerations and some of the trade-offs that you make with the web frameworks in Python. The best way to think about these frameworks is as a spectrum and on one end we have what I refer to as the building block frameworks. These are things like Django. Now what do I mean by building blocks? Well, they're really large sections of functionality you can just drop in or turn on and off. For example, if I want an Excel-like editing experience for all of my database, all my tables in my database, with Django I can just drop that in. I can just turn on a thing and, boom, you have Excel-like editing or maybe Google spreadsheets, more accurate because it's in the web, but you have this sort of big things you can drop in. You want a CMS, drop in the CMS. You want user management, you drop that in there. It comes with a built-in data access layer. This is all a positive thing in a sense that you get these great pieces of functionality. The drawback, and to me just personally, this is a pretty big drawback, is if you don't like the way those things work, if you don't want to follow the prescribed way of doing things that Django offers you, well, too bad. Django's going to make it much harder for you to be different or to choose what I think of as the best of breed building blocks. Do you want to use, say MongoDB and Redis in this unusual way, you want to create your own little management backend, things like that? Well, then you still have all the baggage of Django, but none of the benefits because it doesn't work that way. You're fighting against the system. If you would rather work that way, what you probably want is a microframework. We have one in our framework called Bottle out there, super, super lightweight. It doesn't do a ton, but it basically says, "Here's HTTP request, what do you want to do with that?" That's more or less it. Probably the most popular microframework would be Flask. Flask is very well known and used a lot. Flask is a fine framework. Probably my second favorite of all of them. Flask is good. It's a little more common to use Flask to create web services than it is to create web applications, so HTTP services rather than a bunch of pages, but of course Flask is used for that as well. I'd say Flask offers a little more functionality than Bottle and then we have Pyramid, which has a little bit more support for building full-on web applications than say Flask comes with, in my opinion. There's some benefits. There's some drawbacks. Some of the benefits of Pyramid include, it has better Python 3 support, it has better template support, it's faster, couple of other things. We'll maybe talk about those. It really comes down to the trade-off of do you want a microframework or do you want one of these big building block frameworks like Django, then the difference between, say, Flask and Pyramid is actually much, much smaller. What you learn in one is largely applicable to the other. We're going to see that Pyramid being a microframework will allow us to use something like SQLAlchemy and then if we decide, no actually we'd rather switch to MongoDB, no problem. That's a super, super minor change and everything else will just keep working. That's the really nice stuff of a microframework, real nice features of a microframework is that it lets you pick the various pieces and put them together just the way you want, not the way the framework wants you to or the few choices that framework has made for you.
|
|
|
transcript
|
4:13 |
Let's spend a moment on the Pyramid philosophy and some of the principles that they use to drive their project. I love their tagline: The start small, finish big, and stay finished framework. You'll see that the principles really drive that mission statement there. First of all, they focus on simplicity, and the pay-only-for-what-you-eat approach. And what that means is they've designed the framework so that you can get results even if you have only a partial understanding of Pyramid. You don't need to understand all the stuff in order to make use of it. You just need to know how to, say, create a view and a template and pass data to it. That's really all you need to know if you just want to show simple data, things like that. So they've built this start small and add your understanding to do more and more advanced stuff over time but if you don't care about it, you don't need to know about it, and that's really nice. Being a microframework, they focus on minimalism; the core things that web applications do: mapping URLs to code, templating, security, and static assets. And that's pretty much it. Every web application has to do stuff like this, so that's what they've focused on, doing this really fast and really well. Because Pyramid's relatively small, they can focus on having documentation for everything. So their goal is to make sure nothing goes undocumented. And I could tell you, I've submitted a couple of PRs back to various aspects of Pyramid and a lot of the conversation actually has been, well, what are the effects of this change on the documentation, on the tutorials, and making sure that that's always up to date. Maybe even more so than, is this the right change for the framework? So I can tell you they take documentation super seriously. We talked about focusing on the core things the web framework does, routing URLs, serving static assets, and so on, and I can tell you that Pyramid is one of the fastest Python web frameworks out there. It's super fast, really runs well, and it runs well on Python 3. Some of the frameworks out there, you'll see they run pretty fast on Python 2 but not on Python 3, and nobody wants to be in that place these days, right? Python 2 is on its way out, so having this run fast on Python 3 is really, really awesome. I can give you a little bit of background just running Pyramid for my web applications, both the podcast website and the training website run on Pyramid. I've had various things of those sites each featured on Hacker News, and be on the front page, in the top five or top 10 on Hacker News for a day or two, and that sends a ton of traffic over. You go over to the web server, pull it up, it's like a $5/10 web server, virtual machine, and it's doing like 5/10% CPU usage even though it's just getting hammered with all this traffic. So this framework is super fast and easy on the hardware, and really, really scales in some impressive ways. We obviously want our web frameworks to be reliable, and it's really encouraging that every release of Pyramid has 100% statement coverage for their unit tests. That's awesome. Pyramid is open source and released under a permissive license. You can see your github.com/Pylons/pyramid, and you can go check it out, download it, clone it, do whatever you want so you'll always have access to your web framework. One of the first things that drew me to Pyramid was actually it's super strong support for Python 3. So they made a statement early on saying, Python 3 is the future, Python 3 is going to be our default implementation. Yes, it may still run on Python 2, but we are going to build all of our demos, build all of our examples, and primarily test and develop on Python 3, and make sure things run fast on Python 3. Which they've done really well, and that's awesome. You definitely need to be on Python 3 these days, Python 2 is on its way out really, really soon. At the 2020 deadline, it's basically going unsupported and you do not want to be built on a framework or a runtime that's unsupported. So the fact that Python 3 is the default implementation for Pyramid is just excellent.
|
|
|
transcript
|
3:31 |
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. But what are these building blocks? Everything starts with a route. When we get a request into the web server, 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, 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 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 figure out where does that request get handled within our application. Then we'll have, what Pyramid calls views, but I'd prefer to think of as controllers because of the MVC nature. And these are either methods or they can be classes that process our requests. In our course, we're going to stick to the method style of working, but think of it as just something that you can 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. 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. 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. Maybe you run a podcast and you want to be able to say /<podcast_number> is going to show the details for that podcast. Well, the template itself, the basic HTML structure's always going to be the same, but the various pieces of data, 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. 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. 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. And this is typically done in the form of a Python dictionary. There's also support for static, or cached, assets. So if you've got CSS, JavaScript images, those types of things, one of the easiest things you can do to make your website seem ultra-fast is to cache those really, really carefully. So you'll see that maybe, even though you might return, I don't know, 250K of JavaScript and images on a particular page, if your browser caches that, that site 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, 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. Maybe one has outbound email turned on for all the actions. Like if you click the reset password button |