Flask is a popular micro web framework that provides you with tools, libraries, and technologies for creating web pages, ecommerce applications, and more. There is a common problem for beginners when selecting which framework to learn (for backend web development). Common choices for developers are Flask and Django. I recommend Flask for a beginner, because it is easier to learn and use.
We could use the analogy of a compact home library (when we talk about Flask), while Django would be more of a state library. The point here is that, when you intend to create apps that have minimal functionality, Flask is one of the best options.
However, due to its extensibility, organizations such as Netflix, Airbnb, Uber, Lyft, Mozilla and MIT make use of Flask as one of the technologies when developing their web applications.
By the end of this tutorial, you will properly understand the following:
How to install
- Flask
- to install a virtual environment
- How to structure your web application
- Create and run a simple Hello World program.
. How
.
.
Installing
Flask
The Flask web framework is based on the Python programming language, therefore it will require us to install Python. However, your system might have Python installed; To check if you have Python installed, open your terminal or cmd (on Windows) and type the command:
If you already have Python installed, you should see the version appear in your terminal when you run any of the commands. If you don’t have Python installed, we’ll describe the installation process below.
Python
installation The Python installation process is quite easy. For Windows users, visit the official Python website and download the .exe file. Launch the file and follow the installation process.
For Linux users, on the other hand, Python2 is mostly installed by default, but to update to the latest version, check the official documentation.
Installing
a virtual environment A virtual environment
is a tool that helps keep separate dependencies required in different projects. A virtual environment
is designed to allow you to work on multiple projects that require various dependencies
.
You might have a project that requires the use of SQLAlchemy in your Flask application, but you don’t want this particular dependency to be global across all projects.
A virtual environment would be ideal, as it gives you control over that.
To install your virtual environment, you need Pip installed.
What
is Pip
Pip is a package manager for Python packages and modules. You can follow the step-by-step process provided by the official Python documentation to install pip and a virtual environment.
Installing Flask
Now that you have Python, pip, and a virtual environment installed on your system. We can proceed to the installation of Flask itself.
Here is a
step-by-step process on how to install
Flask: Terminal and
file
directory
The first step is to create
the flask_website directory: Then change the directory
to the flask_website directory created in the previous step
:
Create a virtual environment and store your tools in the “env” folder:
Following the steps above, we have our virtual environment ready for our Flask application, then we will activate it.
Running a virtual environment To activate your virtual environment, from your folder directory,
type the following command This will activate our virtual environment in the “env” folder as we demonstrated in the previous step
.
If you have successfully activated your virtual environment, you should see the word (env) indicating that we are working in a virtual environment.
With all this completed, the fun part can now begin, what you’ve been waiting for, installing Flask!
Installing
Flask
In this step, you will install the Flask web framework
in our virtual environment specifically for the project you are working on. Before installing
Flask, I recommend that you check your version of Python and Pip just to confirm that they are installed and activated in your virtual environment
.
You can proceed to install Flask by running the following command:
Starting the workspace
For the scope of this article, we’ll use Visual Studio Code here. Now, back to your terminal after installing Flask, run the subsequent command to launch VSCode in the current directory of the web application.
Now, let’s proceed to structure the web application.
Structuring your web application Structuring your web application is the first
thing you need to do before delving deeper into the coding process, and below is a schematic representation of how your Flask application should be structured:
The first process in the
Structuring your web
application
is to create an “app”, a run.py file and a “requirement.txt file”. The ”
app” folder structure gives us the flexibility to define our Flask application as a package that can be imported into any part of the application we need.
The run.py file will basically serve as a pointer to Flask, informing you about where the application is and therefore running it. Finally, the “requirement.txt” file houses all the packages used in the project.
To
begin, go back to your terminal and run the following command to generate the “requirements
.txt” file: To
view the packages in use, open the “requirement.txt” file. It should not be more than seven requirements.
Next, you need to create the “app” folder and run.py file at the top level of your working directory.
The file Run.py
This file serves as a pointer to Flask, informing you about the existence of our application and to run the application. In this file, we start by importing the application module from the application folder we created earlier, although this seems confusing, it will soon make sense.
Next, we write an “if” condition to run the application when we run the Flask server, at the
end of this, your run.py file should have code similar to the following:
The application folder
In the previous step, you
wrote the code needed for Python to run the application when we run the Flask server. In
this step, we will completely structure the application folder, and to do that you need to create certain files and folders
.
We start by creating two folders and two Python files, which are: static folder, templates folder, __init__.py
, and views.py.
The __init__.py file contains the code to initialize our Flask application and import our views. In this file, we import Flask, and we also create an application object that is an instance of the Flask class.
Finally, we import the views.py file from the application folder.
The code in the __init__.py file should look like this:
If you remember that in the previous step we imported the application module from the application file
, what we were basically doing was importing the application object into the startup file. Let’s proceed by examining the view.py file.
The file
views.py The views file
contains the paths to the Web pages and can also house the logic of the Web application. In our case, we’ll start with a function that returns a simple “Hello World” message to our browser.
To accomplish this, add the following code to the views.py file:
From the first line of code, we import the app object from the app itself. Next, we define the route using the @app.route decorator, and passing the URL of the web page.
In this case, it’s the homepage and we use the back slash to outline that. Then let’s create an index() function that returns a simple hello world message.
At this stage we can run our application to see how it looks in a web browser.
Running
the application Congratulations, you have come this far, you can run your
application by opening your terminal with the virtual environment active
and run the following command line: When you run the command, your
Flask server should be running and you can click on the link to see if your application works.
If you have carefully followed the process described here, You should see Hello World in your web browser.
Conclusion
By now you should be able to install Flask, structure your web application professionally and create a web page with Flask that prints hello world in your web browser
.
With this introduction, you can now build on it to build web applications
.
You can find the full codebase for this article here.
Peer review Contributions by: Adrián Murage