def setUp(self): """Define test variables and initialize app.""" self.app = create_app() self.client = self.app.test_client self.database_name = "test_trivia" self.database_path = "postgresql://{}:{}@{}/{}".format( 'postgres', '1', 'localhost:5432', self.database_name) setup_db(self.app, self.database_path)
def client(): my_app = create_app({ 'TESTING': True, # Set to True during testing. 'REPOSITORY': 'memory', # Set to 'memory' or 'database' depending on desired repository. 'TEST_DATA_PATH': TEST_DATA_PATH_MEMORY, # Path for loading test data into the repository. 'WTF_CSRF_ENABLED': False # test_client will not send a CSRF token, so disable validation. }) return my_app.test_client()
def test_client(): flask_app = create_app() # Flask provides a way to test your application by exposing the Werkzeug test Client # and handling the context locals for you. testing_client = flask_app.test_client() # Establish an application context before running the tests. ctx = flask_app.app_context() ctx.push() yield testing_client # start testing ctx.pop()
def setUp(self): """Define test variables and initialize app.""" self.app = create_app() self.client = self.app.test_client self.database_name = "test_db" self.database_path = "postgres://{}:{}@{}/{}".format( 'lyuben', 'temp123!', 'localhost:5432', self.database_name) setup_db(self.app, self.database_path) self.new_book = { 'title': 'Time and Power', 'author': 'Petar Deunov', 'rating': 5 }
def setUp(self): """Define test variables and initialize app.""" self.app = create_app() self.client = self.app.test_client self.database_name = "trivia_test" self.database_path = "postgres://{}@{}/{}".format( 'postgres', 'localhost:5432', self.database_name) setup_db(self.app, self.database_path) # binds the app to the current context with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) # create all tables self.db.create_all()
def setUp(self): """Define test variables and initialize app.""" self.app = create_app() self.client = self.app.test_client self.database_name = "bookshelf_test" ##self.database_path = "postgres://{}/{}".format('localhost:5432', self.database_name) self.database_path = "postgres://{}:{}@{}/{}".format( 'Kaustav', 'k', 'localhost:5432', self.database_name) setup_db(self.app, self.database_path) self.new_book = { 'title': 'Anansi Boys', 'author': 'Neil Gaiman', 'rating': 5 } # binds the app to the current context with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) # create all tables self.db.create_all()
def app(): app = create_app() # <<=== A CHANGER return app
from __init__ import create_app, socketio app = create_app(debug=True) if __name__ == '__main__': socketio.run(app)
def client(): app = create_app() # <<=== A CHANGER client = app.test_client() yield client
def main(): app = create_app() app.run(host='localhost', port=8000, threaded=False, debug=True)
def test_client() -> FlaskClient: flask_app = create_app() with flask_app.test_client() as testing_client: with flask_app.app_context(): return testing_client
from __init__ import db, create_app db.create_all(app=create_app())
import sys import models from getpass import getpass from werkzeug.security import generate_password_hash from __init__ import create_app, db from models import User username = input("Enter your username: "******"Enter a new password: "******"Re-enter new password: "******"Did not match." # pylint: disable=E1101 with create_app().app_context(): admin = User.query.filter_by(username=username).first() if not admin: yorn = input(f"Superuser '{username}' not found, create? [Y/N]: ") if yorn in ("Y", "y"): admin = User(username=username, password_hash="") db.session.add(admin) else: sys.exit(0) admin.password_hash = generate_password_hash(new_password) db.session.commit()
from __init__ import create_app application = create_app() application.run()
#! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys from __init__ import create_app, db from models import User, Role, Post from flask.ext.script import Manager, Shell, Server from flask.ext.migrate import Migrate, MigrateCommand app = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(app) migrate = Migrate(app, db) #创建shell命令上下文。。这里的app,db等无需手动在shell中import def make_shell_context(): return dict(app=app, db=db, User=User, Role=Role, Post=Post) @manager.command def deploy(): "Run deployment tasks" from flask.ext.migrate import upgrade from models import User, Role, Post #数据库迁移到最新修订版本 # upgrade() #创建用户角色 Role.insert_roles()
import random import time from celery import Celery from flask import render_template, request, jsonify, session from flask_socketio import SocketIO, join_room from random import randint import celeryconfig from __init__ import create_app flask_app = create_app() flask_app.secret_key = "DataRoadReflect" celery_app = Celery('demo', broker='redis://localhost:6379', include=['tasks']) celery_app.config_from_object(celeryconfig) applogger = flask_app.logger socketio = SocketIO(flask_app, message_queue='redis://') user_1 = { 'id': 1, 'name': 'John', 'role': 'manager', 'age': 60, } user_2 = { 'id': 2, 'name': 'Bob', 'role': 'manager',
def client(): app = create_app() app.config["TESTING"] = True with app.test_client() as client: yield client
from flask import render_template from __init__ import create_app from flask_script import Manager, Shell, Server from flask_migrate import Migrate, MigrateCommand from models import * app = create_app('default') manager = Manager(app) migrate = Migrate(app, db) # db.create_all(app=create_app('default')) # print(app.url_map) def make_shell_context(): return dict(app=app, db=db) manager.add_command("shell", Shell(make_context=make_shell_context)) manager.add_command('db', MigrateCommand) manager.add_command('start', Server(host='0.0.0.0', port=9002)) @app.errorhandler(404) def page_not_found(error): return '未找到你的页面' if __name__ == '__main__': manager.run()
# -*- coding: utf-8 -*- import __init__ import json from flask import Flask app = __init__.create_app() app.run(debug=True)
# -*- coding: utf-8 -*- import os # from flask_script import Manager, Server from flask_script import Manager, Server from __init__ import create_app # Get the ENV from os_environ # 现在的 manager shell 在每次启动的时候,都会获取一次 OS 的环境变量,并以此来创建 app 对象。 # 默认使用 DevConfig 配置 env = os.environ.get('demo_ENV', 'develop') # Create thr app instance via Factory Method app = create_app('main.resources.config.%s' % env) # Init manager object via app object manager = Manager(app) # Create a new commands: server # This command will be run the Flask development_env server # manager.add_command("server", Server(use_debugger=True)) manager.add_command("server", Server(host='127.0.0.1', port=8089)) # new command # manager.add_command("db", MigrateCommand) # @manager.shell # def make_shell_context(): # """Ceate a python CLI. #
# worker_service/intro.py import eventlet import eventlet.wsgi from flask.cli import FlaskGroup from flask import jsonify, Flask # config from __init__ import create_app, setup_logging # initialization setup_logging() socketio, app = create_app() # Run server if __name__ == '__main__': eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 49153)), app)
from flask_script import Manager, Server from flask_migrate import Migrate, MigrateCommand from filters import add_one import models from __init__ import create_app import config from flask_assets import ManageAssets from ext import assets_env app = create_app(config.DevConfig) app.DEBUG = True manager = Manager(app) migrate = Migrate(app, models.db) app.jinja_env.filters['add_one'] = add_one manager.add_command("server", Server(threaded=True)) manager.add_command('db', MigrateCommand) manager.add_command('assets', ManageAssets(assets_env)) @manager.shell def make_shell_context(): return dict( app=app, db=models.db, # db,create_all() User=models.User, Article=models.Article, Comment=models.Comment, Tag=models.Tag, Server=Server)
from flask_script import Manager from config import config from __init__ import create_app config_class = config['development'] app = create_app(config_class) if __name__ == '__main__': manager = Manager(app) manager.run()
#!/usr/bin/env python3 from flask_frozen import Freezer import __init__ freezer = Freezer(__init__.create_app()) if __name__ == '__main__': freezer.freeze()
#!/usr/bin/env python from __init__ import create_app if __name__ == '__main__': app = create_app() app.run()
def app(request): """ Returns session-wide application. """ return create_app(TestConfig)
#! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys from __init__ import create_app,db from models import User,Role,Post from flask.ext.script import Manager,Shell,Server from flask.ext.migrate import Migrate,MigrateCommand app = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(app) migrate = Migrate(app,db) #创建shell命令上下文。。这里的app,db等无需手动在shell中import def make_shell_context(): return dict(app = app,db = db,User = User,Role = Role,Post = Post) @manager.command def deploy(): "Run deployment tasks" from flask.ext.migrate import upgrade from models import User,Role,Post #数据库迁移到最新修订版本 # upgrade()
# _*_ coding:utf-8 _*_ # Filename: main_v2.py # Author: pang song # python 2.7 # Date: 2018/06/24 # 352工具 服务器测试 from __init__ import create_app if __name__ == '__main__': # 局域网访问调试 create_app().run(host='0.0.0.0', port=8190, debug=True) # 本机调试 # app.run(debug=True)
from flask import Blueprint, render_template, flash from flask_login import login_required, current_user from __init__ import create_app, db main = Blueprint('main', __name__) @main.route('/') # home page that return 'index' def index(): return render_template('index.html') @main.route('/profile') # profile page that return 'profile' @login_required def profile(): return render_template('profile.html', name=current_user.name) app = create_app( ) # we initialize our flask app using the __init__.py function if __name__ == '__main__': db.create_all(app=create_app()) # create the SQLite database app.run(debug=True) # run the flask app on debug mode
#!/usr/bin/env python3 import os import sys import tempfile import pytest ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # add the project directory to the sys.path if ROOT not in sys.path: sys.path = [ROOT] + sys.path from __init__ import create_app from dbquery import init_db meownotes = create_app() # test user credentials TEST_USER = "******" TEST_PASSWORD = "******" # a fixture is called by the tests when running # created using the flask documentation http://flask.pocoo.org/docs/1.0/testing/ # START section based on documentation @pytest.fixture def client(): """ Used for all tests; sets up test db """ db_fd, meownotes.config["DATABASE"] = tempfile.mkstemp() meownotes.config["TESTING"] = True
import os from datetime import datetime, timedelta from __init__ import create_app from models import db, Workload from tasks import stop_workload app = create_app() timedelta_hours = int(os.getenv('HOURS_KEEP_DEPLOYMENTS', '1')) with app.app_context(): data = db.session.query( Workload.id, Workload.workload, Workload.workload_type, Workload.namespace).filter(Workload.created < datetime.utcnow() - timedelta(hours=timedelta_hours)).all() for workload in data: stop_workload.send(workload.id, workload.workload, workload.workload_type, workload.namespace)
from flask import Flask import os, config from __init__ import create_app app = Flask(__name__) if __name__ == "__main__": create_app(app).run()