Flask

Flask is a popular Python web framework, meaning it is a third-party Python library used for developing web applications.

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy with the ability to scale up to complex applications.

Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine

Web Framework

A web application framework is a collection of libraries and modules which helps developers write the business layer without worrying about the protocol, thread management, etc.

  • A micro-framework is a minimalistic framework where developers are provided with a lot of freedom to build the web application layer
  • Developer would not need to set up many things in a micro-framework to get the web app hosted and running.

Microweb frameworks, like Flask, provide a minimal set of tools for building web applications, focusing on providing a simple and flexible way to build the application’s core functionality. Full-stack frameworks, on the other hand, provide a complete solution, including all the tools and libraries necessary to build, deploy, and manage a web application.

Flask framework became wildly popular as an alternative to Django projects with their monolithic structure and dependencies.

Blueprints in Flask allow you to organize your application into smaller, reusable components, making it easier to manage and maintain your code. Blueprints can contain routes, templates, static files, and other components that can be used across multiple instances of your application.

WSGI

Web Server Gateway Interface (WSGI) is the standard for Python web application development. WSGI is a specification for a universal interface between the web server and the web applications.

Werkzeug is one of the most advanced WSGI modules that contain various tools and utilities that facilitate web application development. Flask implements Werkzeug.

Jinga2

Jinja 2 is a template rendering engine. It renders the web pages for the server with any specified custom content given to it by the webserver. Flask renders its HTML based templates using Jinja 2.

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<h1>Hello, World!</h1>"

@app.route('/input')
def request_input():
    data = request.args.get('x')
    return "this is my input from url {}".format(data)

if __name__=="__main__":
    app.run(host="0.0.0.0")
# POST
from flask import Flask, request ,render_template , jsonify

app = Flask(__name__)

"""
body > raw > json
{ "operation": "add", "num1": 1, "num2": 2 }
"""
@app.route('/postman_action', methods=['POST'])
def math_ops1():
    if(request.method == 'POST'):
        ops = request.json['operation']
        num1 = int(request.json['num1'])
        num2 = int(request.json['num2'])
        if ops == 'add':
            r = num1+num2
            result = "The sum of " + str(num1) + 'and ' + str(num2) + "is " + str(r)
        elif ops == 'subtract':
            r = num1-num2
            result = "The subtract of " + str(num1) + 'and ' + str(num2) + "is " + str(r)
        elif ops == 'multiply':
            r = num1*num2
            result = "The multiply of " + str(num1) + 'and ' + str(num2) + "is " + str(r)
        elif ops == 'divide':
            r = num1/num2
            result = "The divide of " + str(num1) + 'and ' + str(num2) + "is " + str(r)
            
        return jsonify(result)

Session, Cookies, Global var

  1. before_request
    • The before_request decorator allows us to create a function that will run before each request.
  2. after_request
    • Functions decorated with after_request work in the same way as before_request, except they are run after each request.
  • Give people a tool, and they’ll code for a day
  • Teach them how to build tools, and they’ll code for a lifetime