Basic structure of flask application

Introduction
In this article, we will learn about the enriched parts of a Flask application. The initially and most imperative decision that we will ever make for our project is how can organize it. We should ask ourselves following questions;

What the best Flask project structure is?

What will our project need?

Is it changed from other project’s we’ve done already?

How can we have one single project structure that will make sure our project will not have a problem?

How can that project at the same time will likewise permit us to develop and keep emergent?

Description

Altogether Flask applications need to create an application instance.

The web server clearances all requests it obtains from clients to this object.

Those are for controlling and using a protocol called Web Server Gateway Interface (WSGI).

The application instance is created as an object of class Flask.

That is commonly created as follows:

from flask import Flask

app = Flask(name)

The first necessary argument to the Flask class constructor is the name of the main module or package of the application.

For best applications, Python’s name variable is the right value.

Flask practices this argument to regulate the root path of the application.

Therefore that it far along may find resource files relative to the location of the application.

Routes and View Functions

Clients for example web browsers send requests to the web server.

That in sequence sends them to the Flask application instance.

The application instance requires to identify what code needs to run for each URL requested.

Therefore it saves a mapping of URLs to Python functions.

The relationship between a URL and the function that grips it is named a route.

The most suitable method to define a route in a Flask application is via the app.route decorator.

That is unprotected by the application instance.

That registers the decorated function as a route.

The following illustration demonstrates how a route is stated using this decorator:

@app.route('/')

def index():

return '

Hello World!

Hello, %s!

name
main
name
main

from flask import Flask

app = Flask(name)

@app.route('/')

def index():

return '

Hello World!

name
main

18