Getting Started with Python Flask Framework

Welcome to this Flask tutorial series. In this series, we are going to learn about Flask – Python-based web framework. Flask is one of the most popular web frameworks for Python. Flask is easy to use, flexible and has less learning curve.

You can checkout Flask’s official website to learn more about the Flask.

In this post, we are going to install Flask in our system and code a simple “Hello World” application.

Here is the Video version of the article.

Installing Flask

Before installing Flask, make sure your Python version is upgraded and it is version 2.7 or above. You can check the Python version using the following command.

$ python --version

Configuring Virtualenv

Virtualenv is a python environment builder. It helps you to create multiple Python environments in your system to avoid package and version conflict. This is a very handy and useful tool and I highly recommend you to use it.

If you are running a Linux based system, you can install Virtualenv using the following command.

sudo apt-get install virtualenv

You can also install it using pip.

pip install virtualenv

Now let’s install Flask. Create a new folder and switch to it using the terminal.

Run this command to create a virtual environment.

virtualenv venv

Now activate the virtual environment using the following command.

source /env/bin/activate

You should see (env) on the left side of the terminal.

Install Flask using the following command.

pip install Flask

That’s it. You have successfully installed Flask in your machine.

Building First Flask Program

Before wasting any time, here is our code to return Hello World message when the API is hit.

from flask import Flask
app = Flask(__name__)

def hello_world():
   return 'Hello World'

if __name__ == '__main__':

Save the code in a file name and run it using the following command.


Here is the output in the terminal.

Now open your browser and go to the localhost:5000 URL.

There you go, you just build the first program using the Python Flask framework. Let’s understand the code.

First, we imported the Flask library in our code using the following line.

from flask import Flask

Then, we created a new instance of Flask and provided the name using the __name__ variable.

app = Flask(__name__)

In the next line, we have created a new route. Routes are endpoints that can be used to design the structure of your web application.

def hello_world():
   return 'Hello World'

In the last line, we are are running our app.


In the next post, we are going to learn about Flask routes and how to build API’s using Flask.

Pankaj Kumar
Pankaj Kumar
Articles: 207