Python Flask is a microframework for building web applications in Python.
It is a very flexible and customizable framework that makes learning easy. As a result, it is a popular choice when building web servers. This article introduces Python Flask: what it is, how it compares to other frameworks, and how to create a basic application in Python.
What is Flask?
Flask is a framework for building web servers in Python. Unlike other frameworks, it does not force the developer to conform to a particular directory structure.
In addition, it is unopinionated, allowing you to use whichever tools you prefer to perform actions such as connecting to a database and validating form inputs.
As mentioned, Flask is used to build web servers. A web server is software that responds to HTTP requests with the appropriate data and files.
Flask vs. Other Frameworks
This section will briefly compare Flask to two other popular frameworks – Django and Fast API. We will cover the benefits and limitations of using Flask over the other frameworks.
Flask vs. Django
Flask is a lightweight micro-framework. It offers developers simplicity and flexibility, allowing you to build as you go. However, this means you must do a lot of work yourself.
On the other hand, Django is a “batteries-included” framework that comes with many preconfigured tools. This enables you to build and ship faster. However, Django is not very flexible and has a steeper learning curve.
As a result, Flask is used for small and medium-sized projects, while Django is used for larger and more complex projects.
FastAPI is a relatively new framework for building APIs in Python. It is fast and easy to use. It provides built-in monitoring as a feature. However, it is a new framework with a relatively small ecosystem and community. In addition, its use is limited to APIs.
In contrast, Flask is more versatile as it can be used for both APIs and rendering static files. It is lightweight and relatively easy. However, it is slower than FastAPI.
Flask is best suited for web applications where you must render HTML, while FastAPI should be used to build APIs.
In this section, we will build a simple Flask app to display some HTML and provide data through an API. The point of this is not to provide a comprehensive tutorial to Flask. Rather, the aim is to show you briefly what building a Flask app would look like.
To follow this tutorial, you have to understand Python 3. In addition, familiarity with HTTP would be useful, though not required.
To create an application in Flask, you need to install Python first. If you need a guide on how to do so, here’s a video to guide you:
Create a Virtual Environment
First, create a project folder for the application. I am running my commands in Bash Terminal; therefore, I create a folder using the following command:
Next, navigate inside the folder using the command:
After navigating into the folder, create a virtual environment using this command. This ensures that the dependencies we will install do not conflict with dependencies from other projects.
python3 -m venv venv
This should create a venv folder, which you can see with the command:
Activate Virtual Environment
Activating a virtual environment is easy. Using the Bash terminal, enter the command:
Your terminal prompt should change from this $ to this (venv) $.
With a virtual environment, we can proceed to install Flask. For that, we will use pip:
pip install Flask
That should download some files and install dependencies.
Serving Some HTML
Next, we can begin working on the main.py file. This file will be the application’s entry point.
After creating the main.py file, open it with your preferred editor and add the following code:
from flask import Flask
app = Flask(__name__)
return '<h1>Hello from Flask!</h1>'
In the above code snippet, we started by importing the Flask class. Next, we instantiated the Flask class, passing in the __name__ variable. This is a built-in variable storing the name of the current module. Flask needs this to know where to find files. We stored the instance of Flask in the app variable.
Next, we created a function called index. This function returns the HTML we want to display to the user. Before the function definition, we used the @app.route decorator. This decorator converts a regular function into a Flask view function. This means the function’s return value will be converted to an HTML response.
To run the application, we use the command:
flask --app main run
The –app flag specifies where the application is. In this case, in main.py. The server should start running.
And if you open the browser on http://localhost:5000 or http://127.0.0.1:5000, you should see an h1:
Writing all our HTML in our Python code would generally be bad. Ideally, we would want to separate the Python from the HTML. The next section will look at how to serve standalone HTML files.
Serving an HTML File
In this section, we will write the code to serve an index.html file for our website. First, let’s create the index.html file. We will create this file inside the templates folder because that is where Flask looks for our HTML template
Next, open the file using your preferred editor and add the following code:
Stop the server (Ctrl + C) and restart it using the following command:
flask --app main run
Reload the browser window, and you should see the following.
In this example, we are displaying a static HTML page. With Flask, you can also display templates by creating HTML templates and providing data. These templates follow the Jinja syntax. We will not cover this here for brevity, but I will link some good resources to learn Flask later.
Creating an API Route
Displaying HTML content is one function of web applications. The other is to serve data through an API route. In Flask, it is also easy to set up API routes. Add this code to the main.py file to create an API route at ‘/data’.
Google Docs does a great job of keeping things simple. The default page setup works great for most documents, and common formatting options are right on the toolbar. However, when you need to do some advanced formatting, you’ll need to dig a little deeper.