Before learning extensions, first introduced a few concepts, their own ideas is not very clear, we do not have the means to do too much to explain
Request context and application context
Context Request (request context)
-
request
-
session
Application context (application context)
-
current_app
-
G (processing the request, the object for temporary storage, the reset every request variable)
Request hook
Hook request is implemented in the form of decorators, the Flask support hooks following four request:
-
before_first_request : Run before processing the first request. @ app.before_first_request
-
before_request : run before each request. @ app.before_request
-
after_request (Response) : If there are no unhandled exception is thrown, run after each request. @ app.after_request
-
teardown_request (Response) : after each request to run, even with an unhandled exception is thrown. @ app.teardown_request Notes: need in production mode, that is, when debug = False
Simple example:
# Coding: UTF-. 8 from Flask Import the Flask App = the Flask ( the __name__ ) @ app.route ( " / index " ) DEF index (): Print ( " index performed " ) return ' index Page ' @ app.before_first_request DEF before_first_request_function (): "" " to be executed before the first request processing " "" Print ( " the before the implementation of the first request function " ) @ app.before_request DEFbefore_request_function (): "" " are performed before each request " "" Print ( " before execution of the function Request " ) @ app.after_request DEF after_request_function (Response): "" " in each request (function processing view) after have been executed, provided that the view function does not appear abnormal "" " Print ( " the after request function performed " ) return the Response @ app.teardown_request DEF teardown_request_function (the Response): " "" in every request (view function processing) after They have been executed, regardless of the view function is abnormal, have been executed, work in non-debug mode = False debug "" " Print ( " tearDown Request function performed") if __name__ == '__main__': app.run(host='0.0.0.0')
# Show results (Flask_py) @ Python Python-VirtualBox: ~ / code $ Python hook_demo.py * Running ON http://0.0.0.0:5000/ (Press CTRL + C to quit) the before First Request function is executed before request function execution the index performed after request function performed teardown request function performed 127.0.0.1 - - [24 / Jul-/ 2019 19:39:20] " the GET / index the HTTP / 1.1 " 200 is -
Flask-Script Extended Script
installation
pip install Flask-Script
Add the startup script Manager
# Coding: UTF-8 from the Flask Import the Flask from flask_script Import Manager # start command Management App = the Flask ( __name__ ) # Create Object Manager management class Manager = Manager (App) @ app.route ( " / index " ) DEF index (): return " index Page " IF __name__ == ' __main__ ' : # app.run (Debug = True) # to start the flask through the management objects manager.run ()
Before we start the terminal can view the help information help
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py --help usage: flask_scirpt_demo.py [-?] {shell,runserver} ... positional arguments: {shell,runserver} shell Runs a Python shell inside Flask application context. runserver Runs the Flask development server i.e. app.run() optional arguments: -?, --help show this help message and exit
As it can be seen there are two commands we can choose from, runserver again to view the selected help information help
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py runserver --help usage: flask_scirpt_demo.py runserver [-?] [-h HOST] [-p PORT] [--threaded] [--processes PROCESSES] [--passthrough-errors] [-d] [-D] [-r] [-R] Runs the Flask development server i.e. app.run() optional arguments: -?, --help show this help message and exit -h HOST, --host HOST -p PORT, --port PORT --threaded --processes PROCESSES --passthrough-errors -d, --debug enable the Werkzeug debugger (DO NOT use in production code) -D, --no-debug disable the Werkzeug debugger -r, --reload monitor Python files for changes (not 100{'const': True, 'help': 'monitor Python files for changes (not 100% safe for production use)', 'option_strings': ['-r', '--reload'], 'dest': 'use_reloader', 'required': False, 'nargs': 0, 'choices': None, 'default': None, 'prog': 'flask_scirpt_demo.py runserver', 'container': <argparse._ArgumentGroup object at 0x7f2a3e8db290>, 'type': None, 'metavar': None}afe for production use) -R, --no-reload do not monitor Python files for changes
Start and designated host: 0.0.0.0 port: 12345 and 127.0.0.1 or accessed via local ip
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py runserver -h 0.0.0.0 -p 12345 * Running on http://0.0.0.0:12345/ (Press CTRL+C to quit) 127.0.0.1 - - [24/Jul/2019 20:00:43] "GET /index HTTP/1.1" 200 - 127.0.0.1 - - [24/Jul/2019 20:00:43] "GET /favicon.ico HTTP/1.1" 404 -
The results show:
In addition runserver this option there is a shell of this option
Ipython this is to enter the compiler where you do not need to import flask_script_demo This module can be used directly with it