Flask Learning Tutorial :Hello world from flask

Elsam Atchole
7 min readJan 13, 2023

Hello welcome to the first part of this series of articles dedicated to learning the flask framework and backend development with python. In this chapter we will see together how to set up a Flask project. At the end of this chapter we will launch a simple web application with flask running on our computer.

Python installation

As you have guessed in this series of articles we are going to code in python. If you don’t have python installed on your machine yet, just install it. If your operating system does not have a Python package you can download and install it from the official Python website.

To check if Python is installed on your machine, just open the terminal and type the command python3 or just python. Here is what you should see

The Python interpreter is now waiting for an interactive prompt, it is an interface where you can enter Python statements. In the following chapters, we will see together what this interactive prompt is used for. But for now, you have confirmed that Python is correctly installed on your system.To leave the interactive prompt, you can type exit() and press Enter. on windows or by pressing Ctrl-D on macOs or Linux.

What is Flask?

Flask is a python framework that allows you to create web applications quickly and easily. It is designed to be lightweight, flexible and easy to use.
Flask is based on a request-response model so for each request sent to the server a response is sent back to the client. Flask uses routes to associate URL with functions called views that will handle HTTP requests and return an appropriate response to the client.
Flask offers great flexibility in terms of customization and can be used with a large number of libraries and a wide variety of databases.

Flask installation

Our next step is to install flask in our project but before we go on I’d like to see together the good practices to install python libraries.
In pyhton packages like flask are available on a public repository accessible to all developers called PyPi (Python package Index), to install a package from PyPi on your machine you just have to run the command

By running this command pip will install your package in a folder that will be accessible to all python scripts on your system.Now let’s imagine that you have developed a web application with version 1.4 of flask but in the meantime a version 2 has been released and you now want to launch a new application this time using the latest version of flask and well what will happen is that this new version will overwrite the old one and may break your old application which would be very problematic. What would be ideal is to have version 1.4 available for your old application and version 2.0 available for the new one.

To solve this problem python has a so-called virtual environment.the virtual environment is a copy of your python interpreter that will allow you to isolate the different dependencies your application needs so that each project has its own copy of all the libraries it needs which avoids version conflicts and ensures that each project works correctly even if the dependencies are updated on the global system.

To start, let’s create a folder where our backend will rest I’ll call it flask_api

To set up my virtual environment I just need to type the following command in my terminal

With this command I ask python to launch the package venv which will create a virtual environment named venv.The first venv in the command is the name of the package and the second is the name I want to give to my virtual environment so you can name it whatever you want. Also note that on some operating systems you will have to use the command python3 instead of python.

To activate the virtual environment just type the command

if you are on windows you just have to type the command

Once done you should see on the left side of your terminal the name of your virtual environment as you can see on this picture

Now that our virtual environment is created and activated we can finally install flask

To check that flask is correctly installed you just have to start your python interpreter and type this command

if you don’t get any error it means that flask has been installed correctly congratulations

Hello world

Here is what a minimal flask application looks like:

. First we import the class Flask from flask. An instance of this class will be our WSGI application.

. Then we create an instance of the Flask class that we store in a variable named app. Note that the argument __name__ is the name of the module or package of the application __name__ is a predefined variable that allows Flask to know where to look for resources such as templates or static files including the template folder to return html pages

. We then use the decorator @app.route() to tell flask which URL address should activate our function so that when a user makes a request to that address the function hello() will be executed

. Then we have the hello() function which will display a “hello world” message in the browser. Note that the default content type returned is HTML, so the HTML in the string will be rendered by the browser.

Now all we need to do is to start our server. To start a flask server there are two ways to do it:
1 either by using terminal commands
2 or by using the app.run() function

1 To start flask using the command lines we just need to type the command
flask run this command starts our server which is accessible at the address http://127.0.0.1:5000

2 The other way is to start the server with the method app.run() as shown in this code

`if __name__ == “__main__”:` is a Python idiom that is used to check whether a script is executed directly or imported as a module into another script. If the script is executed directly, __name__ will be set to “__main__” , and the block of code following the if statement will be executed. In this case, app.run() is called, which starts the application. If the script is imported as a module, __name__ will be set as the name of the module, and the code block following the if statement will not be executed.
and to start the server I just have to do `python app.py` but before that I will shut down the server with `CTRL+C`.

Now open your favorite browser and go to http://localhost:5000/hello-world and you will see this

congratulations for holding on until the end we have already made a lot of progress and now we have a good base to continue on.

. We saw together how to install python

. Why it is important to set up a virtual environment before starting a project in python and how to set it up

. We have seen what flask is, what it is used for and how to install it with the pip install command and we have also seen how to set up a flask server in just 5 lines of code.

Don’t forget to follow me on twitter, linkedin and don’t forget to subscribe for the next part which is coming soon.

--

--

Elsam Atchole

Full-stack developer | React JS | Next JS | Node JS | Python | Ruby | SQL | self taught developer who enjoy learning new things and share with the community