In this tutorial, we are going to see the basic Hello World Flask example. It typically contains Flask initial configurations and setup to make the Flask application runnable.

Before going further make sure, you have already installed the Flask in your machine.

Versions:

  • Python 3.8.5
  • Flask 2.0.1

Hello World Flask Example:

Create a new directory flask-hello-world to start the new application.

Enable virtual env:

% mkdir flask-hello-world
% cd flask-hello-world
% python3 -m venv venv
% source venv/bin/activate

Create a python package app to save application source files. In Python, if I say its package it should contain __init__.py file. Hence create __init__.py inside the app folder so the python can understand it as a package.

Project structure:

(venv) flask-hello-world %
.
├── app
│   ├── __init__.py
│   └── routes.py
└── helloworld.py

Create __init__.py

app/__init__.py
1 from flask import Flask
2
3 app = Flask(__name__)
4
5 from app import routes

The above __init__.py file contains the above 3 lines of code, let’s understand them.

  • In the first line, I am importing the Flask class from the flask package.
  • In line 3, I created an instance of the Flask class and stored it in the variable called app. The argument that I passed to the Flask class is __name__ variable from python, this variable provides the name of the package so that the Flask uses this value to find out the location of this package on the disk that way it can locate all other files in the application.
  • In line 5, I imported the routes modules also from this app package, however, this module not existed yet, we are going to create it in a bit.

You may be noticed that there is an import in line 5, which we usually kept imports at the top of the file in python. Actually, this is done intentionally to avoid the common problem with Flask that is a circular dependency. Hence make sure this order matter.

Create routes.py

The routes.py is going to be the module that we are going to write the logic for this application. Here I am going to start with a very simple application that I want to access from the web browser that simply returns Hello World! message in response.

app/routes.py
1 from app import app
2
3 @app.route('/')
4 def index():
5     return 'Hello, World!'

To tell Flask, this is the function index() that I wanted to execute when a user connects to this application from a web browser; we need to use a decorator called @app

You may not be familiar with the decorators in python, but this is a very cool feature that given by the python language, it allows you to enhance the function with additional behaviour. A decorator has to be written above the function and it starts with @ sign.

An @app.routes decorator from Flask creates a mapping between an URL and a function; on the above function I am saying to associate the top-level URL / for my application with the index() function.

To make this work, I need to import the application which was defined in the __init__.py, now we created the hello world app route.

Before we run this application, we need to add a top-level script that represents the application, this goes out of the package that is flask-hello-world directory. I am calling this script as helloworld.py

flask-hello-world/helloworld.py
from app import app

The only thing that the script contains is importing the application, and this is where the Flask is going to obtain our application instance. Now everything is good.

Again! before we run the application, we have to tell the Flask where the application is located. To tell this, we need to define an enrolment variable called FLASK_APP

% export FLASK_APP=helloworld.py # Mac/Linux

> set FLASK_APP=helloworld.py # Windows

helloworld.py is going to be set as FLASK_APP environment variable, which is the module that defines the application.

Run it:

(venv) chan8047@MCF806MD6M flask-hello-world % flask run
 * Serving Flask app 'helloworld.py' (lazy loading)
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Output:

Hello World Flask Example

References:

Happy Learning 🙂