From the course: Full Stack Web Development with Flask

Creating and running a simple Flask application - Flask Tutorial

From the course: Full Stack Web Development with Flask

Creating and running a simple Flask application

- [Instructor] Creating and running a simple Flask application. In this video we're going to create a simple Flask application, first by creating a main module, and in this main module we're going to instantiate a Flask application object and also create a default route to display the data to the UI, which is the data browser window, and then we're going to run and inspect the application to make sure everything is running as expected. So let's go see how this is done. In Visual Studio Code, before we run that though, I want to make sure that we preserve all these packages in a safe file, just in case if we do need to reinstall or if you do move these data or this application to another system, you can reinstall that easily. And that's why I created this file called requirements dot TXT, which is special file very similar to the package.json file, if you are familiar with Node.js application development. Let's go into the command line pressing the control back tick, and again you want to make sure you are in that virtual environment. If you don't you're going to be installing or doing everything in the global space only, so you want to go into that VNV environment. I'm going to go in here VENV, backspace scripts, backspace activate. Now I'm using a Windows machine, so if you're a Mac user or Linux user, you want to prepend all these here with the word source and then space, and then that directory will be okay for Mac users. But for PC, this is all you need to do, and you hit Enter. And now, this VNV indicates that we are now in the virtual environment. So to create that file, we just need to issue this command called pip space through command called freeze, and then the angle bracket here and the file name called requirements.txt, and hit Enter. Now, that should create a file in the enrollment application folder here. If you click on it, you see all the packages that we install in here so that later on if you need to redistribute or move to a different machine, you don't have to install that VNV folder. You just basically take that file and reinstall it and issue all the installer back. Now to install a back again Let's just say if you do that, you just basically do the pip install and then dash R the requirements file TXT. And that should install all the packages to the VNE folder as you can see here. It's not doing it because I already have 'em installed. That's how you do it. Now that is good so we'll keep that. Now we are going to create our main module. Now every dot Py file is considered a module in Flask. And so right in here we going to create a file called main dot Py and some people call it differently. Again, it doesn't matter what you call it. I would treat this as the main entry to our application. We'll just call it main for now. And now I'm going to just close that for now. In here, the first thing that we need to do is we need to import the Flask a class into these application. And you do that by issuing this command called from Flask. import the Flask class. Capital S means a class and then from there, now we have access to all the Flask. Whatever's in the Flask class. And now we need to instantiate object and is typical to have the variable called app, and then we going to assign that to the Flask class. Want to pass in the underscore name, underscore variable that is a special variable name to identify the current application or module that is being rendered or passed to Flask. Just make sure you put that in there. And then now, we are going to create a simple route to run the application. That is by issuing this command both S symbol followed by the app and then route. The forward slash, that is the forward slash in the URL. That is the root directory if you want that to be the root directory. And then you can also add another one if you want app dot route, same index or home or whatever. You can have different types of URLs. These are called decorators. What do they decorate? They decorate the underlying definition or function and this function can be called anything you want, usually index or home or default, whatever that is. That means if I go to browser, if I take the forward slash and our domain, it will load this function and whatever is inside here will be rendered. If I also go to the window and type in slash the word index, it's also going to load that as well. In here then we're going to return a very simple string, and we're just going to say "Hello Earth!" And that's all there is to it basically, it's a very simple application. And go ahead and save that. Now we just need to run it. And so go back to the command prompt. And then write down here and we'll clear this, and then just go on to write the word Flask and run. If everything runs smoothly, you should see that no errors, and it tells us that we're running on port number 5000 at this local host address. And we are running on a debug mode again, by default. It's on a debug mode based on what we set in the environment here. In the old version, you have to set them manually. But now it's good,a go. I'm going to click on here. Control click to load on the browser and we should see that, close this tab, we have the message "Hello Earth!" And if you do a source view, we just see the H1 tag with the text "Hello Earth!" we created. That is our simple application and everything seems working very smoothly. Now the reason why we want to use a debug mode of on, is that when you make changes to your code, you have an auto re-loader activating and running in the background so that you don't have to restart your virtual server again. And now if you don't have that turned on, that mean if I make changes to my file here, then it would not reflect on the web page. Say for example, if I say "Hello Earth!" and put like two exclamation mark if I save the file, you will see that you have something called reloading. That means it's changing the content of your render. And if I go back to the browser, and if I just click refresh, you see that new exclamation mark is active right at that point. That is how you run the application. The next session we'll start creating the enrollment application for our Flask project.

Contents