Coder Social home page Coder Social logo

flask_architecture's Introduction

#Flask Applications Architecture

A Flask App is structured the following way:

  1. Application Instance: All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling using a protocol called Web Server Gateway Interface (WSGI)

The app instance is an object of the Class Flask. It is usually created as follow:

from flask import Flask
app = Flask(__name__)

2)Routes and View Functions: the client sends requests to the web server, who in turn sends them to the app instance. The app instance keeps a map of URLs (received from the web server - clients requests) and Python functions to be run for each URL. This association between a URL and a Python function is called a Route.

The Flask class has a decorator named app.route. This decorator is exposed by the app instance. Each route is defined in Flask using this decorator app.route.

Each route is declared as follow, using the app.route decoratore exposed by the application instance:

@app.route('/')
def index():
	return "<h1>Hello world!</h1>

The return of this function is called the response and it will be passed through the web server to the client browser.

The functions of each routes are called view functions.

When the URL has a variable part, for example you want a page in your app to be called by the user's username, you must use the following syntax in the route decorator:

@app.route('/user/<username>)
def user(name):
	return '<h1>Hello, %s!</h1>' % name

The the URL match the fix portion will call the view function (user(name)) and send the URL part enclosed in the angle brackets as a argument of the view function.

Also, the dynamic part of the URL, supports type definition. For example the route /user/int:id will accept only integers as the id.

Flask supports int, float and path class for the routes. The path type is a string but does not take the slashes as separators but as part of the dynamic content.

3)Server Startup: the application instance has the run method that launches Flask's integrated development web server.

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

flask_architecture's People

Contributors

saulugo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.