Detailed Explanation of Python flask script Module
- 2021-12-12 05:00:35
- OfStack
Directory 1 Role 2 Installation 3 All commands to run are placed in manager. py 4 3 Ways to Create Command4.1 Ways 1 Create Command Subclasses 4.2 Ways 2 Use Decorators @ manager. command 4.3 Ways 3 Use Decorators @ manager. option Summary
1 action
flask-script allows you to execute an flask statement in the form of a command.
Provide the function of inserting external scripts into Flask, such as running a development server, running shell and executing database scripts.
2 Installation
pip install flask-script
3 All commands to run are placed in manager. py
from flask import Flask
from flask_script import Manager, Command
from loguru import logger
# [ Generate 1 A Flask Instances ]
app = Flask(__name__)
# [ Manager Constructor that accepts only 1 Parameters,
# Is Flask Example, Manager Responsible for tracking the whole Command The processing process of ]
manager = Manager(app)
4 3 Ways to Create Commands
4.1 Creating Command Subclasses in Mode 1
# Define the type of command to execute Hello Inheritance Command
class Hello(Command):
# Rewrite Command Class run() Method
def run(self):
''' Command to execute '''
logger.info('sub class of Command')
if __name__ == '__main__':
'''
Will hello Command to join the manager ,
No. 1 1 A parameter is its own command abbreviation,
No. 1 2 Parameters are Command Subclass Hello Example of
'''
manager.add_command('hello', Hello())
manager.run()
Execute python manager on cmd. py hello [hello is short for its own command]
This will print out sub class of Command
4.2 Mode 2 Using Decorator @ manager.command
'''
@manager.command Actually manager It is initialized above Manager Class,
If Manager The instance name of the class is manager_1 The decorator should also be changed to @manager_1.command
'''
'''
Use Decorator @manager.command After, the abbreviation of command is The name of the function hello_command ,
In __main__ Medium You don't need to In Pass manager.add_command() Will Command abbreviation is added to command Instance in the
'''
@manager.command
def hello_command():
logger.info('@manager.command')
if __name__ == '__main__':
'''
In __main__ Medium You don't need to In Pass manager.add_command() Will Command abbreviation is added to command Instance in the
'''
manager.run()
Execute python manager on cmd. py hello_command [hello_command is short for its own command]
This will print @ manager.command
4.3 Mode 3 Using Decorator @ manager.option
'''
-u Object that is a command parameter Abbreviation, --username Object that is a command parameter Full name
(Similar to Linux Parameters of the command, parameter abbreviations For 1 A - The parameters are all called -- ),
Whether it is a parameter's Abbreviation, or Full name Must be based on - At the beginning, but The full name can also be used only by 1 A - At the beginning,
However, it is recommended to use Two - At the beginning, because it is more in line with the coding specification, dest The value of must be the same as that of hello_option Function (that is, command) Input reference name
Weighing Exactly the same, because dest Is to represent the value of this parameter and pass it to the hello_option The one that commands the function to enter the parameter; default Denote
Parameter, that is, if you execute the python manager.py hello_option When executing a command, take no parameters, and use the default of the parameters
Value
'''
@manager.option('-u', '--username', dest='username', default='zhangsan')
@manager.option('-p', '--password', dest='password', default='123456')
def hello_option(username, password):
logger.info('@manage.option')
logger.info(f'username = {username}, password = {password}')
if __name__ == '__main__':
'''
In __main__ Medium You don't need to In Pass manager.add_command() Will Command abbreviation is added to command Instance in the
'''
manager.run()
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!