1. 引入方式
# 方式1
from manager import Manager
# 方式2
from flask_script import Manager, Shell
2. Demo源码
# -*- coding: utf-8 -*-
from flask_script import Manager, Shell
from flask import Flask
import users
def _make_context():
try:
import ipdb as pdb
except ImportError:
import pdb
return dict(pdb=pdb)
app = Flask(__name__)
manager = Manager(app)
manager.add_command('shell', Shell(make_context=_make_context))
manager.add_command('user', users.manager)
if __name__ == '__main__':
manager.run()
# -*- coding: utf-8 -*-
from flask_script import Manager, Command, Option
# ===== Variable =====
manager = Manager(description="user cmd")
# python manage.py user hello1
@manager.command
def hello1():
print 'hello boy'
# python manage.py user hello2 -n bb
@manager.option('-n', '--name', dest='name', default='joe')
def hello2(name):
print 'hello', name
# python manage.py user hello3 -n cc
class Hello(Command):
def __init__(self, default_name = 'kevin'):
self.default_name = default_name
def get_options(self):
return [
Option('-n', '--name', dest='name', default=self.default_name)
]
def run(self, name):
print 'hello', name
manager.add_command('hello3', Hello())
$ python manage.py user hello1
hello boy
$ python manage.py user hello2 -n bb
hello bb
$ python manage.py user hello3 -n cc
hello cc
$ python manage.py runserver
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
...
$ python manage.py shell
# 问题,引入pdb之后,shell有什么用
3. 参考文档