Hunnar Blog

Hunnar Blog

Introduction to Flask - A Beginner Approach

Introduction to Flask - A Beginner Approach

Mudassar Hayat's photo
Mudassar Hayat
·Apr 25, 2022·

6 min read

Subscribe to our newsletter and never miss any upcoming articles

Introduction

A website is an essential part of every business. A business can attract users through the website. By having a website, a business shows its presence to customers throughout the world. A website is also essential for a Data Scientist or Machine Learning Engineer to show their output to the end-users or customers. Let's say you have built a Face Recognition system using some Deep learning algorithm. Showing the output and demonstrating the code to the end users using a terminal / Command Prompt would not make any sense because maybe the end-user is not from a Computer Science background. Maybe he/she is unfamiliar with the programming language and they don't know what is terminal, what is PyCharm, Spider, Jupyter Notebook, etc.? (These are the IDE for code development)

In this case, a website can be helpful to show the code output to the end-users. Because a website is a Graphical interface anybody can interact with it easily without having prior knowledge. Now the question is how can a Data Scientist / Machine learning engineer like you quickly deploy his/her model and show the output to the end-users? To answer the question, we have an amazing and efficient web framework called FLASK which can help us to achieve it.

Table of Content

Following are the content that we will discuss in this article.

  • Who and When

  • What is Flask?

  • Who can Learn Flask?

  • Why to Learn Flask?

  • Benefits of using Flask

  • Other Web Frameworks

  • Prerequisites

  • Installing Flask

  • Implementation of Flask

  • Conclusion

Who and When

Armin Ronacher an Australian open-source developer, developed Flask on April 1, 2010. A famous python project Pocco was also led by Armin Ronacher. you can further read about Pocco using the following link pocco

What is Flask?

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies, and several common framework-related tools. Wikipedia

flask.png Flask is very easy to learn and quick to implement. It is preferred by many Data Scientists as it is quick to implement and easy to show a demo of their models. As Flask is written in Python, Developers who are familiar with Python find it no difficult to learn Flask. Though Flask has no additional tools or libraries that a normal framework would have, it makes it easy to implement other services like databases.

Who Can Learn Flask?

This is an important question in every beginner's mind either he/she will learn the flask. What are the prerequisite?. Let be clear if you are familiar with the following things then welcome, you are in the right place and Flask is for you.

  • Do you have basic knowledge and understanding of Python

  • Do you have hands on Html (hyper text markup language)

if you have familiar with the above things then you are good to go with the flask.

Why to Learn Flask?

Flask is very popular in the market nowadays because it is Micro Web Framework and it is very powerful. You can develop a full dynamic Web Application using a flask. Following are the famous tech companies that are using flask for their businesses.

  • Netflix

  • Reddit

  • Airbnb

  • Lynt

  • Mozilla

  • Uber

tech.png

Benefits of using Flask

Following are the core benefits of using Flask.

Easy to Use: The Flask framework is easy to understand, which makes it perfect for beginners. The simplicity in the flask framework enables the developer to navigate around and create the application easily.

Very Flexible: Most of the Flask components can be altered. It allows users to customize the website.

Testing: It allows unit testing through its integrated support, built-in development server, fast debugger, and restful request dispatching. Makes testing much faster. source: medium.com

benefits.png

Other Web Frameworks

Following are the other python web frameworks in the market:

Untitled design (2).png

Prerequisites

Following are the things you will need to run flask.

  • Python

  • Flask

  • Virtual Environment (Recommended)

Installing Flask

Before installing the flask make sure you have python installed. You can verify this by checking the python version in your cmd (command prompt) like this.

mudas@DESKTOP-0L2GNEQ MINGW64 ~
$ python --version
Python 3.9.7

In case python is not installed then you can install python using the following link

Finally, it's time to make our hands dirty by installing a flask. Hold on let's first create a virtual environment. If you are unfamiliar with the virtual environment, this link is for you [virtual env]

Creating virtual environment for our flask web application.

Step1

pip install virtualenv

Step2

virtualenv flask_env

Here flask_env is your environment name, you can also give it any name.

Step3

Activating Virtual Environment

For Linux base system

source flask_env/bin/activate

For Windows

flask_env\Scripts\activate

Step4

After activation install Flask

pip install flask

Implementation of Flask

Now we have installed all required dependencies and packages. For the implementation part, I am using Visual Studio code editor. You can use any code editor like PyCharm, Spider, Atom, Jupyter Notebook, etc. You can download Visual Studio code editor (Vs Code) using the following link. Before starting flask implementation just take a look at our project directory structure.

.Flask_APP
├── app.py
└── templates
    └── index.html

We make a folder Flask_APP, in this folder, we have one file app.py which is our main file for the flask web application. And one folder named templates which has one file index.html. All the web pages we create must be inside the templates directory. Now jump into the app.py file and import Flask and render_template for rendering the web pages. Make sure to activate the environment which we create above.

app.py


from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
     return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

Now jump into the index and type some content that you want to display on the web page. In my case I m just printing our first "Hello to Flask" but you are free to display anything you want.

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Flask App</title>
    </head>
    <body>
        <h1>Hello to Flask</h1>
    </body>
</html>

Now just open the terminal and type

python app.py

You can see this type of output in the 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: on
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

Congratulations we make it. Our first flask app is up and running. Now click on the given link http://127.0.0.1:5000 or type localhost:5000 in your localhost browser to see your flask app rendering the HTML web page. The output will be.

Hello to Flask

Conclusion

In this article, We have seen flask implementation and its importance in today's World. We cover all the "W" which most beginners have in their minds like What is flask? Who can learn flask? Why learn flask? What are the Benefits of the flask? We even saw major tech companies also using flask for their businesses. I hope this article was useful to you all. If you have any questions/queries regarding the flask feel free to ask. Will see you in my next article until then Eat, Sleep, Code and Repeat...

 
Share this