87. flask-script component of flask, flaskflask-script

Source: Internet
Author: User

87. flask-script component of flask, flaskflask-script

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

Flask Script works in a similar way as Flask itself. You only need to define and add commands called by the Manager instance from the command line;

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

1. Create and run commands

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 the invocation of all the commands called in the command line and the handling process;

Manager has only one parameter-Flask instance, which can also be a function or another Flask instance returned;

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() 

Create and add commands;

2. There are three methods to create a Command: Create a command subclass, use the @ Command modifier, and use the @ option modifier;

1) first -- create Command subclass

The Command subclass must define a run method;

For example, create the Hello command and add the Hello command to the Manager instance;

From flask_script import Manager, Serverfrom 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 () # The command is runserverif _ name _ = '_ main _': manager. run ()

Run the following command:

Python manager. py hello
> Hello world

Python manager. py runserver
> Hello world

2) second -- use 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() 

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

Python manager. py hello
> Hello world

3) method 3: Use the @ option modifier of the Command instance

In complex cases, @ option is recommended;

There can be multiple @ 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') # Use-n, you can also use -- name, dest = "name" The name Of The command entered by the user as a parameter to pass to the name@manager.option in the function ('-U',' -- url ', dest = 'url', default = 'www .csdn.com ') # The command can either use-u or -- url, dest = "url" the url of the command entered by the user is passed to urldef hello (name, url) in the function as a parameter: 'Hello world or hello <setting name> 'print 'hello ', name print url if _ name _ = '_ main _': manager. run ()

Run 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.