From the course: Full Stack Web Development with Flask

Configuring Flask

- [Instructor] Configuring Flask. In this video, we're going to configure Flask by creating a project folder, and then also create a virtual environment where we'll be installing the Flask framework into that virtual environment. We'll also be setting up some environment variables and install a Flask-WTF extension for our web forms. And finally, we want to create a file called requirements.txt to track all the dependencies. So let's see how this is done. So here I am in the command line, I'm going to go ahead and create a folder called enrollment. I'm going to cd right into it. And I've typed the word code with a dot to load Visual Studio code. Again, I'm using Visual Studio code, you can use any ID you choose. All right, so in here, I'm going to load the command prompt as well. We can go here, press the Control + Back take or if you go to the View and there's a Terminal menu here, click on that to load the terminal, just make sure you're in the right folder here. Okay, so the first thing we want to do is to create this virtual environment. And the reason why you do that is you want to sandbox your application, so that it's not interfering with other application that may be running at the same time. 'Cause the last time we installed Flask, we installed it in the global space. So that means that it's usually used when you create something in a production environment. So, since we'll be creating our application in it, or any other application as well, you want to sandbox it. So that's by creating a virtual environment using a module called venv. And there's another one by a third party called virtualenv. Either one is fine. So I'm going to use the venv which is a module that is packaged along with Python. And that's by using this command called py-m for the module and the module name is venv, and the next parameter here is just the name of the folder or that environment you want to call it. You can call whatever you want. It's very common to call it the same name, some people just call it env like that. Okay, which way is entirely up to you. So I'm going to call it just venv and hit Enter. And that's going to create a folder for us here. And this folder, it will create that virtual environment. So now, what we need to do is, we need to go into that virtual environment to install Flask. Just make sure you always be in the virtual environment before you install. If you don't, then whatever you install is going to go right into the global space, okay? So, that means we have to activate that by going into this folder, and instead of Scripts folder, there's a file called activate. Okay, we need to issue the command and invoke this command here shell from the command line. So just by going to the venv, backslash, and then scripts and then backslash and then activate. I'm using Windows so the Mac or a Linux machine will be a little bit different, you'll be using source instead of the venv here. So once you are here, then you can see that, if it's working correctly, you see the word venv. That's the folder that we created here. That means that we are now inside this virtual environment. And anything we do at this point will be installed to that virtual environment. So, now we're going to install Flask in here. As you can see, there's no Flask inside here. If you go into the library, under the site packages, there's nothing in there, really just the pip packages, that's all. So, let's go install Flask again, go pip install flask. And that should install Flask into that environment. So as you can see, it's been added here amongst other tools as well. The jinja template is also here. OK? So, we also need to install one more package the WTF, so let me just clear this for now. And again, just pip install and then flask-wtf. And that will install the form module for us. So later on when we create our forms, we can use that to create it for us very, very quickly. Okay, so that's good to go. Okay, make sure you're in the enrollment folder and create a file called .flaskenv and hit enter. Okay, this is a environment file, where you can actually set a key value pair for some environment variables. Now this option was not available in the previous version of Flask after version one for now we have this option, this capability, so it's pretty handy. In here, I'm just going to create two variables that we'll use. And the first one is called FLASK_, all caps, ENV for the environment and move to set that to the development environment. Now in the previous version, it was defaulted to this development environment. But version one is defaulted to the production environment. So, when you run your application, you want to run in a development environment. And this is why you will set up inside this file. The second variable is called the FlASK_APP. And this is the starting point of our application. So, I'm going to call my main.py. Okay, you can call it whatever you want, but this is the main entry to our application. And I don't have it yet, but we will create that later on. So, these are the two variables I will create for this one for now. And if you do need to add more later on, you can always come back and add it in. So save the file. And then one more thing that we need to do is that in order to run these variables here, you also need a special package to invoke with this. If you don't install the package, it's not going to let you do it. So let me clear this screen down here in the bottom, and we need to install a package called flaskpython-dotenv. And you can check to see if you already have in here. So just type in the more pip list, and then you can see the list here. If you do have it, then it should say Python-dotenv. I don't have it, so I need to install that. So let's install that by issuing the command pip install Python-dot for dotenv. Hit Enter and that's going to install the package for us. And if you list that again, it should be in there now. Okay, so this is the one that we want. This one will allow us to invoke this environment variables. Okay, it's prompting this message here should upgrade my pip, so I'm going to do that right now just in case. And I think we should be good to go. So now once you are done with the environment, you also want to deactivate the environment and to do that anywhere in your code, you just issue the command deactivate. And that should be moved that green color on the left side again you can see that now we're outside of the environment and we are done at this point. So in the next video, we will create and run our first simple Flask application.

Contents