Flask is a lightweight WSGI Web application framework. It is designed to make getting started quick and easy to scale to complex applications. It started as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python Web application frameworks.

Flask provides advice, but does not enforce any dependencies or project layout. Developers choose the tools and libraries they want to use. The community also provides many extensions that make it easy to add new features.

Reference documentation flask.palletsprojects.com/en/1.1.x/

Pros: Easy to get started, customization, WSGI compatibility

Cons: No database help

Starting from creating a Flask project using PyCharm, the initialization of the new project is complete

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World! '


if __name__ == '__main__':
    app.run()
Copy the code

At the top of the file, introduce a third-party library

Then we define a variable app and assign it to the Flask constructor, which means that the app will receive the name from the script, which can be changed to any character value

@ decorator, which is used to define routes. The next hello_world function returns the string ‘Hello World! ‘

We can also add a route and function:

@app.route('/super_simple')
def super_simple():
    return 'Hello from planetary API'
Copy the code

Now run the server to see what it looks like. Python3 app.py and open http://127.0.0.1:5000/ to see Hello World! Open http://127.0.0.1:5000/super_simple see Hello from planetary API

Use Postman tests

Add Settings for auto-hot loading (failed) return jSON-formatted file, add Jsonify, and assign the return value to Message

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def hello_world():
    return jsonify(message='Hello from planet')

if __name__ == '__main__':
    app.run()
Copy the code

Improve return results

1 Add a status code

@app.route('/not_found')
def not_found():
    return jsonify(message='oops, 404'), 404Copy the code

2 Adding Parameters

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/parameters')
def parameters():
    name = request.args.get('name')
    age = request.args.get('age')
    if age < 18:
        return jsonify(message = 'sorry' + name + 'you are not old enough')
    else:
        return jsonify(message = 'welcome' + name + 'enough')


if __name__ == '__main__':
    app.run()
Copy the code

But there was an unexplained error

TypeError: '<' not supported between instances of 'NoneType' and 'int'
127.0.0.1 - - [11/Jan/2020 10:57:41] "The GET/HTTP / 1.1 the parameters" 500 -
[2020-01-11 10:57:42,373] ERROR in app: Exception on /parameters [GET]
Traceback (most recent call last):
  File "/ usr/local/lib/python3.7 / site - packages/flask/app. Py." ", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/ usr/local/lib/python3.7 / site - packages/flask/app. Py." ", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/ usr/local/lib/python3.7 / site - packages/flask/app. Py." ", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/ usr/local/lib/python3.7 / site - packages/flask / _compat. Py." ", line 39, in reraise
    raise value
  File "/ usr/local/lib/python3.7 / site - packages/flask/app. Py." ", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/ usr/local/lib/python3.7 / site - packages/flask/app. Py." ", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "app.py", line 17, in parameters
    if age < 18:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
127.0.0.1 - - [11/Jan/2020 10:57:42] "The GET/HTTP / 1.1 the parameters" 500 -
Copy the code

Database ORM (SQLAlchemy)

Open the setup item interpreter plus button to add

API security

There’s Flask-Login, which handles logging in, logging out, and session management.

There’s also Flask-User, which handles user registration, login, logout, and role based security, like the kind I mentioned earlier.

When evaluating these plugins, remember traditionally Flask is used to make traditional template based websites. I don’t like the idea of storing session data in my API projects, so I don’t use anything that requires session management.

flask-jwt

Forget password to retrieve flask-mail using mail

mailtrap

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

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. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.

Flask model select flask.palletsprojects.com/en/1.1.x/pa…