Flask for Beginners

Praveena S
featurepreneur
Published in
3 min readMay 13, 2021

What is Flask?

Flask has gained significant popularity as a Python web framework. It is widely recognized as a versatile and powerful third-party library, specifically designed for creating web applications with ease and efficiency.

Installing Flask on Linux:

For those who are using Ubuntu, installing Flask is a breeze. Simply execute the command provided below:

Step 1: $ sudo apt install python3-flask
Step 2: $ sudo apt install python3-pip
Step 3: $ pip3 install flask

Once you have successfully installed Flask, you can easily verify the version by executing the following command:

$ python3 -c "import flask; print(flask._version_)"

Simple Hello World Application in Flask

The first statement in the above code sample imports the “Flask” class from the “Flask” module. Once imported, you can use all methods available in the “Flask” class.

In the following statement, a new instance of the “Flask” class is created and the name of the file is supplied to it as an argument. If you are just using a single “.py” file without an additional “__init__.py” file typically used while creating a file structure for a Python package, the name will have a hardcoded value of “__main__”. The “__name__” argument tells Flask where to look for files related to the current Flask application being executed. You can also supply your own custom path to your main Flask file instead of supplying a “__name__” argument. But typically, most developers use “__name__” only and this seems to be standard practice.

Next, a “decoration” “app.route” is attached to the “hello_world_app” function. Decorating functions extend or modify the function they are attached to, without actually changing them. Here, “app.route” decorator is used to specify the URL path where the attached function will run. The “/” symbol tells Flask to execute a function on the “/” URL, which stands for the “root” domain or “root” URL. For instance, if the URL for your app is “app.com”, the function will be triggered for the “app.com” URL. You can change it to something else as well. For instance, by using a decorator “@app.route(‘/helloworld’)”, you can map the function to the “app.com/helloworld” URL.

Finally, the “hello_world_app” function is used to return the content that you want to display in a web browser when your web app is running.

Running a Flask App

In order to effortlessly launch your Flask app on a Linux platform, simply execute the prescribed command presented in the following format:

$ export FLASK_APP=main.py
$ flask run

Change the “main.py” name as needed to match it with your own “.py” file where you have written your Flask code. The second command runs your Flask app:

$ FLASK_APP=main.py flask run

Once you execute these commands, expect to witness output similar to the following:

To witness your application in action, simply open the URL provided in the terminal using a web browser. It’s as easy as that!

Conclusion:

Flask, an incredible web application development framework, is a great tool for building all kinds of applications — from simple to highly complex. This article specifically focuses on guiding you through the creation of a straightforward “Hello World!!” app. So, buckle up and get ready to dive into the world of Flask!

--

--