Loading, please wait...

What is Python Flask?

Jun 26, 2020 #Python#PythonFlask, 1244 Views
Explanatory working of Flask

What is Python Flask?

Flask is lightweight, WSGI, and micro-framework. It does not have its own libraries. It depends on the developer to choose the required tools. It has no pre-existing libraries which provide a common function. Flask allows to you build a web application. The web application includes blogs, web pages, or other commercial websites. It has almost no dependencies to external libraries.

Flask installation

Flask package is installed from the Python Package Index (PPI). Create a directory in which are you going to work and then install a flask package. Also, install flask-sqlalchemy to establish a connection in the database.

Sample of “Hello world” application

We can create our application with the help of following steps:

  • Create the structure of the application.
mkdir -p hello/{sample, file_section}

This is statement mkdir is used to create a directory. The sample

mkdir -p hello/{sample, file_section}

is the name of the folder where you put your template. File_section is the name of the folder where we put the required files such as images, javascript.

  • Create the file of an application

        cd hello

        cd hello.py

       write the following code in the hello.py file

import flask

# Create the application.
APP = flask.Flask(__name__)

def index():
    """ Displays the index page accessible at '/'
    return flask.render_template('Hello.html')

if __name__ == '__main__':
  • Create the template with .html extension

           vim sample/Hello.html

       write the following HTML code inside this file

<html lang='en'>
  <meta charset="utf-8" />
  <title>Hello world!</title>
  <link type="text/css" rel="stylesheet"
        href="{{ url_for('file_section',
              filename='Hello.css')}}" />



Last, step is to run the application

  Python Hello.py

Advantage of Using Flask for the web application:

  • It is a lightweight framework.
  • It is not dependent on us to find out the security bugs.

 A disadvantage of Using Flask for a web application:

  • Sometimes, it takes so much time for the addition of required plugins.

Related Article