Introduction to Flask – Install and Create a Hello World app in Flask

Flask Hello World

In this article, we will introduce the Flask web framework and how to install it. Later, we will code a simple Hello World App in Flask.

What is the Flask Web Framework?

Flask is a web framework written in Python, used to create web applications. It is a “micro” web framework, which means that it does not require any extra tools or libraries. 

Note that “micro” does not mean that it can’t do things that other frameworks can. It just means that you, as a developer, can decide everything in Flask, like what database to use, etc.,

Some of the default decisions that Flask has, like the Template engine used, etc., also can be changed if needed.

Hence it does not include already-existing third-party libraries or functionalities such as database abstraction layer, form validation, etc.

In a nut-shell, Flask is a developer-friendly “micro” web framework !!

Installing Flask into your system

We can install it into our device using the pip command. In the console/terminal, run the command

pip install flask

That’s it !! Flask has been installed.

Building a Hello World App

Now we will create a small webpage which, when accessed, will show “Hello World.”

Hence create a Python file and write the following codes in it. I have used the name “app.py”; you can keep it anything you want.

1. Import Flask and create a Flask app object

The first thing we do in the file is to import Flask

from flask import Flask

Then we create a Flask app object:

app = Flask(__name__)

Here Flask acts as a class object. We send in the special python variable __name__ as argument Flask class. This special variable essentially gives each file a unique name.

So when we run the Flask application, for Flask to know that this application is running in a specific unique place, we use this special variable.

2. Write the code to print hello world

After creating the Flask object, we now need to display the Hello World message on the browser. So add the code:

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

The decorator @app.route(‘/hello’) indicates the URL endpoint of the webpage. And what the webpage will show is written in the function below.

3. Make the server run on a specific port

We use the following code to start the server and mention the port , the server will run on.

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

LocalHost implies that the server will run in your local computer(at port =5000) itself.

4. Complete code for hello world app in Flask

That’s it. The final code for the file:

from flask import Flask

app = Flask(__name__)

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

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

5. Run the Hello World App

That’s it with the coding part. Now in the terminal, run the Flask file:

python app.py

Here I have used “app.py,” which is my file’s name. In your case, you will use your file name.

Terminal
Terminal

Now copy the URL shown above (http://localhost:5000/). This is going to be your Host Website’s URL. We will be using the route endpoint with this host website URL, to open our webpage.

Hence, hit the URL “http://localhost:5000/hello” to see the web page.

webpage
webpage

Congratulations, We have successfully built our first Flask webpage

Conclusion

That’s it for this tutorial, guys!! In the upcoming tutorials, we will learn more about the Flask framework.

So stay tuned and keep coding !!