From the course: Full Stack Web Development with Flask

Running and configuring the development server - Flask Tutorial

From the course: Full Stack Web Development with Flask

Running and configuring the development server

- [Instructor] Running and Configuring the Development Server. In this video we're going to take a look at creating a config file, which is the configuration module to store some major configurations that we use across the application. We'll also be creating a route py module file to store all the routing patterns in our application. And then we'll need to also modify the init file to reflect these changes in the application. So let's go and see how this is done. Okay so back here in the Visual Studio code, I'm going to create a file in here right in the enrollment folder, so make sure you're in the root directory of the application, and create a file called config.py. Okay in here we're going to import the os, and below that we're going to create a class Config. And there other ways to do this, I just happen to chose this. I like it this way because you want to put all your configurations in a class and then you can access all these properties through this classical Config. And you see some applications, if it's a small application, you can leave this parameter in the main py or in the init file, but you can also you know move it out and put into their own separate classes like I'm doing here. So in here we're going to create this parameter called the SECRET_ KEY. Now this one here, we're not going to use it now yet, but we will later on. This is something very common in Python in Flask. It's a special key that is used as signature key to make sure that anything you sent across the server is not been altered or been hacked by you know hackers. Like for example when you create forms or when you set cookies, and chances are I mean possibly that those cookie files might have been altered. So if that's the case then it's going to use that key to check against a cookie file that was created by this application to make sure that nothing's been altered, okay? If it's been altered then the cookie file, whatever that is, the session file is no longer valid and it toss that away I mean for security reasons. So it's very common to do that as well. So you'll hear and say secret key is equal to the os.environment.get the secret key and then you have put this secret key constant here. And over here you can say "or" you can put a very unique phrase over here. You're going to put a secret string here, okay? This could be a very long string of whatever you want, anything you can put here is completely valid. It's just secret string that'll be hashed and then use that attached to a cookie file or session variable so that it safely secured, okay? And I put too many equal signs over here, okay. So we just need that for now, and later on we can add more stuff in here like the databases and so forth. We can come back here again and add another string for the database connection. Okay so I want to just save that for now. And then I'm going in to create another file, and this time it's going to be inside the applications folder module here. And so we're going to do file here and call this one routes.py, and this where all the routing takes place. So for example, you don't want to put all your routing in here, right? You want to put everything inside the route. So let's do that my moving all of these here. You want to cut this out, Control X to cut it out, and put it inside the route here, okay? But to do that I need to access this file, this app object here. So I need to go into the route and import, I mean from the application import app, okay just like this is very similar to the main file as you can see it's same thing. I need to access this app object here. That is why I need that there. And then back in this init file, I need to include that in here as well. So you need to import that app route back here again. So this of kind of a little bit weird how Python or Flask is doing this way because virtually they calling itself, so it's going in a circle here. So it's almost like a loop, and which is why the preferred way to do that is that we import the routes back into the init app here. You want to put it at the bottom here so it doesn't circulate, okay? So as you can see I'm calling here, I'm going to say from the app, which is the same file itself here, I mean from application, sorry. Application import the routes. Okay the routes here refers to the routes file here itself. Okay? So I'm doing that and let me save that here. So now all my routing patterns will be created inside this routes.py so I don't have to do everything inside the init anymore because that can grow very, very huge. And so this is the preferred method to do it because now you contain all related code, all this business logic at their own files. So another words, in the object-oriented paradigm this is a separation of concerns, okay? So you put all the routes here, you import back into the init file here, and then now it's just a work just like before. Okay? So let me go ahead and save this, and go to the command line, and then make sure that that we are still able to run this application. So again go to the virtual environment. I missed the "s" for scripts, okay? And then just type in the Flask run, and if all goes well it should work. And if you load it on the browser, and it still work just like before. Okay? So that is all we need to do at this point. And later on, the next sections or next videos we're going to create some these static files also so running pages and the templates as also. And when we get there then this config file becomes very handy along with the routes py file become very useful as well. In the next video we'll start by creating the landing page, the home page of our application.

Contents