Developing Python Flask Hello World App from scratch

Developing Python Flask Hello World App from the scratch
Develop Simple Hello World Flask Application – TutLinks

In this basic Python Flask tutorial, we will see how to install Flask. We also implement a simple Hello World application using Flask. We will also learn how to run the Hello World Python Flask application.

Key Takeaways

By the end of this tutorial you will be able to

  • Setup an environment to create a basic hello world Flask application.
  • Create, activate and set up the virtual environment.
  • Write a simple hello world Flask application and understand the code line by line.
  • How to Run simple Flask app and Stop the application.

Before you begin

Ensure you have latest Python programming language installed on your PC. If you are on a Windows OS, check this video that details How to Install Python on Windows 10 OS. Flask is a module or library or package implemented in Python programming language to develop web applications that scale. In order to install Flask, you need pip to help you install it. pip is the python package installer that comes default with installation of Python.

Set up Working Directory

To set up the virtual environment run the following commands from the command terminal

mkdir hello-world-flask
cd hello-world-flask

Setup the Virtual Environment

To create our flask hello world app, we will setup a virtual environment. To understand why we need a virtual environment you can go through the detailed tutorial that explains Working with Virtual Environments in Python for Beginners.

Create the Virtual Environment

From Windows OS, from the command prompt type the following command to create the virtual environment.

python -m venv env

From Unix based OS, from the command prompt type the following command to create the virtual environment.

python3 -m venv env

Activate the Virtual Environment

Unix OS

source ./env/bin/activate

Windows OS

env/Scripts/activate

Once you have successfully activated the virtual environment, you should see the command line terminal shows the name of the virtual environment in closed parentheses as a prefix to the active terminal input.

This is how the terminal with activated virtual environment looks in Windows OS

(env) c:/Users/navule/hello-world-flask

Ensure that you perform the further steps with virtual environment set active. Do not close the command prompt. If you want to work on the same project, you need to set the current working directory to the hello-world-flask and then activate the environment and work as usual.

Upgrade pip

Before installing Flask with the help of pip, ensure to upgrade the version of pip by running the following command.

python -m pip install --upgrade pip

Install Flask

Now install Flask module with the help of pip

pip install flask

Freeze dependencies to a file

Run the following command to freeze all the Python dependencies for Flask application.

pip freeze > requirements.txt

Develop Simple Hello World Flask Application

As we have successfully setup the environment for developing a basic Python Flask application, we will proceed with the implementation.

Create a python file

From the command line terminal run the following command to create a python file named app.py where our hello world program will be written.

echo '' > app.py

Write Python Flask Hello World App in just 6 lines of code

You can run a simple hello world Python Flask application in just 6 lines of code.

# app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

app.run(host='0.0.0.0', port=5000)

All you need to do is copy those 6 lines of code in a file named app.py or file name of your choice with extension py.

You notice that the Hello World! web application is dependent only on Flask framework and python programming language. Nothing Else!.

Detailed explanation of our Hello World Python Flask Application

Following is the line by line explanation of what is happening in our app.py Hello World Python Flask application.

  • Line 2: we are importing flask module’s class named Flask
  • Line 3: we are initializing the Flask module with the name of our package or application. __name__ evaluates to app in our case as the module or file name is app.py
  • Line 5: @app.route(‘/’) indicates that we have a route in our application that when access with / will invoke the functionality it is decorated with. In our case its a method named hello.
  • Line 6: def hello(): is the standard way to declare a method that holds what this routine should do. def is the python standard keyword to declare a method and hello is the name of the method. Empty parentheses indicate that the hello method does not require any arguments to be passed in order to invoke it. A method definition should end with colon and hence we have def hello():
  • Line 7: Our application when accessed with route ‘/’ should be returning the text ‘Hello, World!’.
  • Line 9: Finally we are configuring our Flask app to run on localhost. 0.0.0.0 defaults to localhost. You can also mention ‘localhost’ or ‘127.0.0.1’ as the value of host argument. But it is preferred to set it to ‘0.0.0.0’ as it doesn’t require further setting when we deploy it to the various environments. In addition to setting the value of host, we are also configuring our application to run on a specific port 5000. If port 5000 is not available, you have the option to set it to any other free port available in order to run our Flask app.

Run the Flask Application

Type the following command from your command prompt and your web app should be up and running.

python app.py

You will see the following output from the command terminal.

* Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

To access the application, from any browser of your choice type in the following url

http://localhost:5000/

And the web page should show you the output as Hello, World!

Stop the Flask Application

Once you are done, stop the application running by hitting the keys Ctrl+C

Deactivate the Virtual Environment

It is good to deactivate the virtual environment after you are done working on the project. Type the following command to deactivate the Python virtual environment

deactivate

Demo Python Flask Hello World Application 📺

Watch the real demonstration of Python Flask Hello World Application 📺

Video Tutorial

Full Source code

Full Source code of this tutorial is available here

Congratulations 🎉, you have mastered how to implement a simple Python Flask hello world application and run it on a PC.

Bookmark 🔖 (Ctrl + D) this page as a quick reference on how to Develop a Python🐍 Flask Hello World App from scratch.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
2 months ago

[…] the sequence of steps below to setup the Hello World Flask application in VS […]

trackback
1 month ago

[…] creating a new Python project or on an existing Python project. You also can create a basic Hello World Flask based Python app and try configure Python plugins for VS Code […]