flask-script component

The Flask Script extension provides the ability to insert external scripts into Flask, including running a development server, a custom Python shell, scripts to set up databases, cronjobs, and other command-line tasks that run outside of web applications; make scripts and system separate;

Flask Script works similarly to Flask itself, just define and add commands to be called by the Manager instance from the command line;

Official documentation: http://flask-script.readthedocs.io/en/latest/

1 Create and run the command

First, create a Python template to run the command script, which can be named manager.py;

In this file, there must be a Manager instance, the Manager class tracks all the commands and processing procedures invoked on the command line;

Manager has only one parameter - the Flask instance, which can also be a function or other returning a Flask instance;

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

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

Second, create and join the command;

There are three ways to create a command, that is, create a Command subclass, use the @command modifier, and use the @option modifier;

The first - create a Command subclass

Command subclasses must define a run method;

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

copy code
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 one: 
manager.add_command( ' hello ' , Hello())  
 #Custom command two: 
manager.add_command( " runserver " , Server()) #The command is runserver if __name__ == ' __main__ ' :  

 
    manager.run()  
copy code

Execute the following command:

python manager.py hello
> hello world

 python manager.py runserver 
> hello world

The second - use the @command modifier of the Command instance

copy code
#-*-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()  
 
copy code

This method creates the command in the same way as the Command class creates;

python manager.py hello
> hello world

The third - use the @option modifier of the Command instance

In complex cases, it is recommended to use @option;

There can be multiple @option option parameters;

 
copy code
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 use either -n or --name, dest="name" The name of the command entered by the user is passed as a parameter to the name in the function
@manager.option( ' -u ' , ' --url ' , dest= ' url ' , default= ' www.csdn.com ' ) #The command can use either -u or --url,dest=" url" The url of the command entered by the user is passed as a parameter to the url in the function 

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

It works like this:

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

 

The Flask Script extension provides the ability to insert external scripts into Flask, including running a development server, a custom Python shell, scripts to set up databases, cronjobs, and other command-line tasks that run outside of web applications; make scripts and system separate;

Flask Script works similarly to Flask itself, just define and add commands to be called by the Manager instance from the command line;

Official documentation: http://flask-script.readthedocs.io/en/latest/

1 Create and run the command

First, create a Python template to run the command script, which can be named manager.py;

In this file, there must be a Manager instance, the Manager class tracks all the commands and processing procedures invoked on the command line;

Manager has only one parameter - the Flask instance, which can also be a function or other returning a Flask instance;

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

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

Second, create and join the command;

There are three ways to create a command, that is, create a Command subclass, use the @command modifier, and use the @option modifier;

The first - create a Command subclass

Command subclasses must define a run method;

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

copy code
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 one: 
manager.add_command( ' hello ' , Hello())  
 #Custom command two: 
manager.add_command( " runserver " , Server()) #The command is runserver if __name__ == ' __main__ ' :  

 
    manager.run()  
copy code

Execute the following command:

python manager.py hello
> hello world

 python manager.py runserver 
> hello world

The second - use the @command modifier of the Command instance

copy code
#-*-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()  
 
copy code

This method creates the command in the same way as the Command class creates;

python manager.py hello
> hello world

The third - use the @option modifier of the Command instance

In complex cases, it is recommended to use @option;

There can be multiple @option option parameters;

 
copy code
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 use either -n or --name, dest="name" The name of the command entered by the user is passed as a parameter to the name in the function
@manager.option( ' -u ' , ' --url ' , dest= ' url ' , default= ' www.csdn.com ' ) #The command can use either -u or --url,dest=" url" The url of the command entered by the user is passed as a parameter to the url in the function 

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

It works like this:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325051440&siteId=291194637