def test_client(): flask_app = create_app() flask_app.debug = True testing_client = flask_app.test_client() ctx = flask_app.app_context() ctx.push() yield testing_client ctx.pop()
# -*- coding: utf-8 -*- from gevent import monkey monkey.patch_all() import os import logging.handlers from abc import ABC from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from gunicorn.app.base import BaseApplication from gunicorn.six import iteritems from multiprocessing import cpu_count from example import create_app, db from config import Config app = create_app(Config) manager = Manager(app) migrate = Migrate(app, db) manager.add_command('db', MigrateCommand) def _rotating_file_handler(): """ 创建logging.handlers.RotatingFileHandler对象 """ file_name = os.path.join( app.config.get('LOG_DIR_PATH', os.path.join(os.path.dirname(__file__), 'logs')), 'app.log')
#!/usr/bin/env python # coding: utf-8 from flask_script import Manager from example import create_app app = create_app() manager = Manager(app) if __name__ == '__main__': manager.run()
broker=app.config['BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery flask_app = create_app() celery = create_celery(flask_app) @celery.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): # Calls reverse_messages every 10 seconds. sender.add_periodic_task(10.0, reverse_messages, name='reverse every 10') # Calls log('Logging Stuff') every 30 seconds sender.add_periodic_task(30.0, log.s(('Logging Stuff')), name='Log every 30') # Executes every Monday morning at 7:30 a.m. sender.add_periodic_task(
def main(): from example import create_app bolinette = create_app() bolinette.exec_cmd_args()
def flask_app(): flask_app = create_app() yield flask_app
import sys import os from click import echo from example import create_app app = create_app('development') @app.cli.command('urlmap') def urlmap(): """Prints out all routes""" echo("{:50s} {:40s} {}".format('Endpoint', 'Methods', 'Route')) for route in app.url_map.iter_rules(): methods = ','.join(route.methods) echo("{:50s} {:40s} {}".format(route.endpoint, methods, route)) @app.cli.command('ipython') def ipython(): """Runs a ipython shell in the app context.""" try: import IPython except ImportError: echo("IPython not found. Install with: 'pip install ipython'") return from flask.globals import _app_ctx_stack app = _app_ctx_stack.top.app banner = 'Python %s on %s\nIPython: %s\nApp: %s%s\nInstance: %s\n' % ( sys.version,
# /run.py from example import create_app app = create_app('dev') if __name__ == '__main__': app.run()
def app(request): """Session-wide test `Flask` application.""" return create_app()