from flask import render_template from flask_apscheduler import APScheduler from flask_script import Manager from news import create_app from flask_restful import Api from news.apps.newsflow import NewsFlowList from news.apps.jobs import CustomScheduler # 环境切换 dev/prod app = create_app('dev') # Restful API api = Api(app) # Flask-script manager = Manager(app) # 总入口 @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return render_template('index.html') # 路由 api.add_resource(NewsFlowList, '/api/newsflow') api.add_resource(CustomScheduler, '/api/scheduler') if __name__ == '__main__': manager.run()
from news import create_app, db, models # 导入models from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from news.models import User from flask import session, current_app from datetime import datetime, timedelta from random import randint #工厂方法,获取APP app = create_app("dev") manager = Manager(app) Migrate(app, db) manager.add_command("db", MigrateCommand) # 定义方法:创建管理员对象 # @manager.option() 给manager添加一个脚本运行的方法 # 在终端运行命令: python index.py create_superuser -u admin -p 123456 @manager.option('-u', '--username', dest="username") @manager.option('-p', '--password', dest='password') def create_superuser(username, password): admin = User() admin.nick_name = username admin.mobile = username admin.password = password admin.is_admin = True try: db.session.add(admin) db.session.commit()
#!/usr/bin/env python import os from news import create_app, db from news.models import Source, Caption from flask_script import Manager, Shell #That is start file for program initialization news = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(news) #This makes next names available in shell mode def make_shell_context(): return dict(news=news, db=db, Source=Source, Caption=Caption) manager.add_command("shell", Shell(make_context=make_shell_context)) if __name__ == '__main__': manager.run()
from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from news import app, db, create_app # define the manager manager = Manager(app) Migrate(app, db) manager.add_command('db', MigrateCommand) @app.route('/index') def index(): return 'index' # develop app for development or production app = create_app('development') if __name__ == '__main__': manager.run()