Introduction to the Flask Tutorial Series
What is going on guys? Welcome back. This video today is going to be the first episode of a new tutorial series here on Neural 9 about Flask development. Now, Flask is a Python backend web development framework and in this tutorial series, I want to cover the basics and also some intermediate concepts. So how to create a new project, what virtual environments are, how to work with routes and forms, but also stuff like session management, static files, user authentication, connecting to databases, working with blueprints, and so on.
And if this tutorial series is popular, if you guys like it, I'm going to probably make also an advanced Flask tutorial series in addition to this one. Today we're going to talk about how to create a first project, how to create a virtual environment, and what Flask basically is and how it works. So let us get right into it.
What Is Flask?
Alright, so we're going to start in this first episode today by creating our first simple Flask application, a simple hello world application, just to get to know the process of creating and running a Flask app. So it's going to be very beginner friendly and simple.
Now, as I already mentioned, Flask is a Python web development framework, and it's oftentimes also referred to as a micro-framework or a micro web framework because it's so simple and minimalistic compared to something like Django. When you create a new Flask application, you don't have any functionality out of the box. You don't have an authentication system or anything like that. You basically have to implement everything from scratch or you have to install additional packages that implement the functionality. We're not going to go into the advantages and disadvantages, we're not going to compare here Django versus Flask. We're going to go into Flask, but it's important to know that Flask is very simple and minimalistic. You can basically build an application with a single file. You don't have some project structure that has to be in place in order to run a simple hello world project. We're going to see that this is the case in a second here.
Creating a Python Virtual Environment
So the first thing we're going to do is we're going to create a virtual environment, and the reason you want to create a virtual environment is because you want to have an isolated project where you only have the packages and also the package versions that are relevant to this particular project. There are many reasons you want to do that, but one reason is that it's very convenient to then create a requirements.txt file to then deploy the application, to containerize it, and so on. But there are also other benefits.
So what we're going to do first is we're going to open up a command line, so on Windows CMD, on Linux and Mac the terminal, and we're going to navigate to the directory that we're going to be working in. In my case this is the current directory, and here now we're going to create our Flask project directory. So we're just going to create a directory. I'm going to call it now first-app. And in this directory here, in this first-app directory, we're going to create a virtual environment. For this you don't need to install any Python packages. This is something that works with Python out of the box, it's part of the core Python installation. And what we need to do is we need to say python or python3 depending on your operating system, -m venv and then the name of the virtual environment directory. I like to call it .venv which makes it a hidden directory.
Configuring and Activating the Virtual Environment
And once you have this, once this is created, you can see that we have this virtual environment here. And depending on your IDE, depending on the editor that you're using, you will have to configure that now the environment that is being used is this environment, because by default it's going to use the environment it always uses. In my case here in PyCharm, what I have to do is I have to click here on the Python interpreter. And I have to—oh actually I'm blocking this with my camera. Let me just move this for a second. I have to click on the Python version down here, I have to add a new interpreter, a local interpreter, and then basically I have to go to Virtualenv environment, existing, and then I basically have to look for this. So I have to in this case, I have my prepared projects here. I have to go to Python, current, venv, bin and then I want to use python3. This is the executable or the binary I'm interested in. I click on this, I click okay, and now my PyCharm uses this virtual environment.
In addition to that, the terminal that I want to use, I also want to activate this environment in. So I want to say, at least on Linux and Mac, source and then venv/bin/activate. And now you can see it's using the venv environment. How can you check if this is actually working? You can just type pip or pip3 freeze, and you're going to get a list of all the packages that are installed. In this case, no package is installed.
Installing Flask and Creating the App File
Now once you have the virtual environment running and activated, what you can do or what you have to do is you have to install Flask. So pip or pip3 install flask. And then this is going to install everything that's needed for a basic Flask application. I can run pip freeze again and now you can see I have a couple of packages here installed in this environment.
So after this, what we want to do is we want to create a Python file called app.py, and this is actually all we're going to need for this video today. We don't need any directory structure, we don't need models, we don't need anything like that. We can just have a simple Python file and this is going to be our whole application. Of course, when you build more complex projects, you still have a directory structure, you have templates, you have blueprints, and so on. We're going to talk about this in future episodes, but for a simple Flask application, one file is enough.
Building a "Hello, World!" Application
So what we're going to do here now is we're going to import from flask Flask with a capital F. So from flask import Flask. And then we're going to create the application by saying app = Flask(__name__). So this is how you create the application.
And now what we do with this application is we add endpoints or routes, you could say. We're going to talk about routes in the next episode, but for this video, we're going to just create a simple default route, a simple index route. So what we're going to do is we're going to use the decorator. So @app.route and we're going to specify the rule, so basically the path that this route is going to belong to. And the default is just /. So @app.route('/') and then below that, we want to create a function that is going to return something, which is what we're going to see, the HTML code we're going to see when we go to that endpoint. So here we're going to say now def index(): and here you can return something. Now later on, we're going to render HTML templates, we're going to do it professionally, but for now, you can just return any text. You can just return "Hello, World!" for example. And this is what you're going to see when you go to that specific endpoint.
Running the Flask Development Server
Now, this is now the endpoint. To actually run the application, what we're going to do is we're going to say if __name__ == '__main__': that's just a basic Python main section. What we're going to do here is we're going to say app.run() and we can provide a couple of parameters here. The three important parameters that we're going to provide here are the host, the port, and whether we want to run this in debug mode or not. So we can say host= and what you want to do is you always want to run the application on the local host. So on the host of the machine, not necessarily on localhost, but on the local IP address. So if you're running this on a server, you can provide the IP address that you can find with the terminal, so for example something like 192.168 and so on, or you can provide localhost if you just want to run this locally, or you can provide both by passing 0.0.0.0. This is going to basically use the local IP address and localhost. So you can keep it simple and just provide this.
And then you can also provide a port. I'm not sure what the default port is, actually see what the default port is. And then you want to provide debug=True. Now you don't want to provide debug=True when you're actually running this on a server, when you're actually deploying this. But while you're developing this application, you want to run it in debug mode because then you don't have to constantly restart the server, you can just keep it running, change the code and it's going to update automatically. And you're also going to see the error messages and stuff like that. But you want to turn this to false once you deploy the application.
So right now I can just run this. I think the default port is, yeah, 5,000, but we can also change this. For example, you can say port=5555 if you want to. Then you can run this and you can see it's running now on localhost and also on the private IP address. And when I click on this, it will open up a web browser, and this web browser goes automatically to the default route / and I get "Hello, World!" as an output here. And you can of course also change this to be HTML code, so you can say here <h1> and then </h1>. Yeah, and then this is going to, when you save this it will update because you're running this in debug mode. I can click on it and you can see now I have a heading here. So this is the basic idea of a simple Flask application.
Managing Dependencies with requirements.txt
What we're going to do now in the next episodes is we're going to add more routes, we're going to handle different types of requests, we're going to add dynamic routes, we're going to add post requests and status codes and templates and blueprints and all that. But this is where it all starts. This is the simple, basic hello world application in Flask.
And of course, what you can also do is you can run this in the terminal. So you can just say python3 app.py. This also works. And just as one thing up front, you can now take all the packages—let's say this is now your application, you want to deploy it somewhere—all you have to do, for example, to push it to GitHub so that people can use it, you want to do something like pip3 freeze and then you want to pipe this with the greater than sign, with the closing angle bracket, you want to feed this into a requirements.txt file. And when you have that, you have this requirements.txt file and then people can just go ahead and say pip3 install -r requirements.txt and they can install all the packages that you have installed in this particular virtual environment.
Conclusion and What's Next
So that's it for today's video. I hope you enjoyed it and I hope you learned something. If you like this tutorial series, if you want to see more Flask content, if you maybe want to see also an advanced tutorial series, let me know in the comment section down below. Depending on how much you guys enjoy these videos, how much you watch them, I'm going to make more videos. I'm definitely going to finish this tutorial series, but as I said, if you guys watch this a lot, like this a lot, I'm going to make more videos, more advanced videos on Flask development as well. So thank you very much for watching. See you in the next video, and bye.