Flask es1EN script module

  • 2020-11-18 06:22:05
  • OfStack

The Flask Script extension provides the ability to insert external scripts into Flask, including running 1 development server, 1 custom Python shell, setting up database scripts, cronjobs, and other command line tasks running outside of web applications. Separate the script from the system;

Flask Script works in a similar way to Flask itself, simply defining and adding commands called from the command line by the Manager instance;

The official document: http: / / flask - script. readthedocs. io/en latest /

Create and run the command

First, create an Python template and run the command script named ES28en.py.

In this file, you must have an instance of Manager, where the Manager class tracks the execution of all commands and processes invoked on the command line.

Manager has only one argument -- an instance of Flask, or a function or other return instance of Flask;

Call manager.run () to start the Manager instance to receive commands from the command line;


#-*-coding:utf8-*- 
from flask_script import Manager 
from debug import app 
 
manager = Manager(app) 
 
if __name__ == '__main__': 
 manager.run() 

Second, create and add commands;

There are three ways to create the command: subclass Command, use the @ES50en modifier, and use the @ES51en modifier.

Type 1 - Create the Command subclass

The Command subclass must define 1 run method;

Example: Create Hello command and add Hello command to Manager instance;


from flask_script import Manager  . Server
from flask_script import Command 
from debug import app 
 
manager = Manager(app) 


class Hello(Command): 
 'hello world' 
 def run(self): 
  print 'hello world' 

# Custom command 1 : 
manager.add_command('hello', Hello()) 
#  Custom command 2 : 

manager.add_command("runserver", Server()) # Command is runserver
if __name__ == '__main__': 
 manager.run() 

Execute the following command:

[

python manager.py hello
> hello world

python manager.py runserver
> hello world

]

Type 2 - USES the @command modifier of the Command instance


#-*-coding:utf8-*- 
from flask_script import Manager 
from debug import app 
 
manager = Manager(app) 
 
@manager.command 
def hello(): 
 'hello world' 
 print 'hello world' 
 
if __name__ == '__main__': 
 manager.run() 

The method creates the command in the same way as the Command class does;

[

python manager.py hello
> hello world

]

Type 3 - USES the @option modifier for the Command instance

In complex cases, @ES103en is recommended;

You can have multiple @ES106en option parameters;


from flask_script import Manager 
from debug import app 
 
manager = Manager(app) 
 
@manager.option('-n', '--name', dest='name', help='Your name', default='world') # The command can be used -n, You can also use --name . dest="name" The name of the command entered by the user is passed to the function as an argument name
@manager.option('-u', '--url', dest='url', default='www.csdn.com') # The command can be used -u, You can also use --url,dest="url" Of a command entered by the user url Is passed to the function as a parameter url

def hello(name, url): 
'hello world or hello <setting name>' 
 print 'hello', name 
 print url 
 
if __name__ == '__main__': 
 manager.run() 

The operation mode is as follows:

[

python manager.py hello
> hello world
> www.csdn.com

python manager.py hello -n sissiy -u www.sissiy.com
> hello sissiy
> www.sissiy.com

python manager.py hello -name sissiy -url www.sissiy.com
> hello sissiy
> www.sissiy.com

]

Related articles: