flask 通过flask-script生成指令(flask 36)

2019-08-13 14:46:35 浏览数 (1)

app.py

from flask import Flask from flask_bootstrap import Bootstrap from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from flask_script import Manager

app = Flask('sayhello') app.config.from_pyfile('settings.py') app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True

db = SQLAlchemy(app) bootstrap = Bootstrap(app) moment = Moment(app) manager=Manager(app)

import views, errors from commands import *

if name == 'main': manager.run()

commands.py

from app import db,manager from faker import Faker from models import Message

@manager.command def hell(): print("hello")

@manager.option('-c','--count',dest='count',default=20,help='Quantity of messages,default is 20.') def forge(count): db.drop_all() db.create_all()

代码语言:javascript复制
faker=Faker()
print('Working...')

for i in range(int(count)):
    message=Message(name=faker.name(),body=faker.sentence(),
                    timestamp=faker.date_time_this_year())
    db.session.add(message)
db.session.commit()
print('Create %d fake messages' % int(count))

执行:

python app.py --count 1

程序启动

python app.py runserver -h 0.0.0.0 -p 8888

0 人点赞