What is Flask? How To Create The Flask Application?

Create Flask – a micro web framework written in Python programming language. It is classified as a micro-framework because it.

Create Flask – a micro web framework written in Python programming language. It is classified as a micro-framework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, as well as other components where pre-existing third-party libraries provide some common functions. Moreover, it supports extensions that can add application features as well as implemented in it. Extensions exist for object-relational, form validation, upload handling as well as various open authentication technologies and several common frameworks related tools. The extension updated far more regularly than the core Flask program.

Create

Flask is a web framework which provides you with tools, libraries, and technologies. That allows building a web application as well as this web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or for a commercial website.

Flask is part of the categories called the micro-framework. Micro-framework is generally a framework with external libraries. It also has pros and cons. Pros are that the framework is light even there is a little dependency to update and watch for security bugs whereas cons are sometimes you will have to do work by yourself as well as it increases yourself the list of dependencies by adding plugins which are quite annoying sometimes.

Related:-Upgrade to Windows 10 for free right now

Features of Flask application:

  1. Configuration management. An application has a lifecycle with specific stages—at the very least, it would be development, testing, as well as deployment.
  2. Self-hosting Flask application with Gunicorn.
  3. Serving static files request with Nginx.
  4. An app inside Docker containers on a dedicated Linux server.
  5. Configuring and deploying a PostgreSQL database for the application.
  6. Set-up a task queue to handle long-running tasks.

Above there is the basic knowledge about the Flask application, now let’s come to the second thing that how to create the Flask program? As the flask is written in a python programming language as well as classified into micro-framework.

Create a flask program:

Let’s start by creating an application code that which help you to make the flask application.

  • First, create a structure.
mkdir -p hello_flask/{templates,static}
  • As you see below, the basic structure of your application in Flask.
$ tree hello_flask/
hello_flask/
|-- static
`-- templates
  • To create the application file.
cd hello_flask
vim hello_flask.py
  • Put following code like given below to operate the flask.
import flask


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


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


if __name__ == '__main__':
    APP.debug=True
    APP.run()
  • Her to learn how to use HTML in flask to build an application as well as create the template index.html.
vim templates/index.html
  • Following codes by using HTML.
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset="utf-8" />
  <title>Hello world!</title>
  <link type="text/css" rel="stylesheet"
        href="{{ url_for('static',
              filename='hello.css')}}" />
</head>
<body>

It works!

</body>
</html>
  • Run the application
python hello_flask.py

Related:-The Best COVID-19 Tracking Apps and Websites

USING ARGUMENTS IN YOUR FLASK APPLICATION.

  • Here , add following entry as given in the code below.
@APP.route('/hello/<name>/')
def hello(name):
    """ Displays the page greats who ever comes to visit it.
    """
    return flask.render_template('hello.html', name=name)
  • As well as, create the template in HTML.
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset="utf-8" />
    <title>Hello</title>
    <link type="text/css" rel="stylesheet"
          href="{{ url_for('static',
               filename='hello.css')}}" />
</head>
<body>

      Hello {{name}}

</body>
</html>
  • Run the Flask application
python hello_flask.py