def app(): app = Flask(__name__, template_folder='../templates') app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL_TEST') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = '@##&6cweafhv3426445' app.config['REMEMBER_COOKIE_HTTPONLY'] = False app.config['SESSION_COOKIE_HTTPONLY'] = False app.config['TESTING'] = True app.url_map.strict_slashes = False app.json_encoder = EnumJSONEncoder login_manager = LoginManager() login_manager.init_app(app) db.init_app(app) app.app_context().push() install_database_extensions() install_models() install_routes() install_local_providers() app.mailjet_client = Mock() @app.route('/test/signin', methods=['POST']) def test_signin(): from flask import request identifier = request.get_json().get("identifier") user = find_user_by_email(identifier) login_user(user, remember=True) return jsonify({}), 204 return app
def make_app(config='config.py'): app = Flask(__name__) app.config.from_pyfile(config) # LOGGING CONSTANTS formatter = logging.Formatter( "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s") handler = logging.handlers.RotatingFileHandler(app.config['APP_LOG_FILE'], maxBytes=1024 * 1024 * 100, backupCount=20) handler.setFormatter(formatter) handler.setLevel(app.config['APP_LOG_LEVEL']) app.logger.addHandler(handler) app.logger.setLevel(app.config['APP_LOG_LEVEL']) # Allow CORS CORS(app, supports_credentials=True) # Initialise the app with the database setup db.init_app(app) # Setup the Flask-JWT-Extended JWTManager(app) # Degine all the moduls from app.modules import tickets # Register the blueprint of each module app.register_blueprint(tickets.module) return app
def app(): app = Flask(__name__, template_folder='../templates') app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) with app.app_context(): install_models() return app
def create_app(config_name): app = Flask(__name__, instance_relative_config=True) app.config.from_object(app_config[config_name]) app.config.from_pyfile('config.py') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.register_blueprint(api_bp) db.init_app(app) return app
def init_db(app): import models from models.game_char import GameChar from models.game import Game from models.player import Player from models.req import Req from models.user import User from models.db import db db.init_app(app) models.Base.metadata.create_all(bind=db.engine) db.session.commit()
def initialize_extensions(app): """ Initializes flask extensions. Args: app (Flask): flask application. """ # Initialize SQLAlchemy instance. db.init_app(app) # Allow cross-origin requests. cors.init_app(app) # Apply database migrations. migrate.init_app(app, db)
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) jwt = JWTManager(app) @jwt.token_in_blacklist_loader def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] return RevokedTokenModel.is_jti_blacklisted(jti) @app.before_first_request def create_tables(): db.create_all() from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from models.db import db db.init_app(app) return app
def create_app(self): app = make_app(config="test_config.py") db.init_app(app) Migrate(app, db) return app
app.config.from_object(Config) app.register_blueprint(eventbp) # GitHub API if not app.config.get('GITHUB_CLIENT_ID') or not app.config.get( 'GITHUB_CLIENT_SECRET'): logger.error( 'Set GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET in .env or environment variables' ) sys.exit(-1) github.client_id = app.config['GITHUB_CLIENT_ID'] github.client_secret = app.config['GITHUB_CLIENT_SECRET'] # SQLAlchemy (needs to be run on import for pythonanywhere) db.app = app db.init_app(app) # create database tables for models with app.app_context(): db.create_all() @app.template_filter() def datetimesince(datestr): """Returns the difference between the given datetime string and now as text. E.g. 13 minutes ago """ try: if isinstance(datestr, str): datestr = dt.parse(datestr) now = datetime.now(timezone.utc)
import connexion from connexion.resolver import RestyResolver from models.db import db, configureAppForDB if __name__ == '__main__': connexionApp = connexion.App(__name__, specification_dir='swagger/') connexionApp.add_api('v1.yaml', resolver=RestyResolver('api.v1')) # Configure MongoEngine # app here is the Flask instance configureAppForDB(connexionApp.app) db.init_app(connexionApp.app) db.create_all(app=connexionApp.app) connexionApp.run(port=9090)
""" Forms HTTP path for view functions """ return root_view + '/' + name_html URL_update_test = url_view('update_test') URL = url_view('calc1') URL_update = url_view('update') # Flask initialisation app = Flask(__name__) app.config.from_pyfile('app.cfg') # SqlAlchemy initialisation app.db = db db.init_app(app) def fix_currency(currency, errors, db_rates): """ """ if not currency: return 'RUR' # default currency if currency not in db_rates: errors.append('Currency {} doesn\'t exist'.format(currency)) return 'RUR' # default currency return currency # View forms @app.route('/calc1') def calc():
pymysql.install_as_MySQLdb() from flask_script import Manager # 可以将flask项目用命令行的方式进行数据库的更新和其他的一些操作,比如说启动服务器 from flask_migrate import Migrate, MigrateCommand # 导入migrate相关的模块,数据库迁移相关模块 from config.app import app from models.db import db from models.model import * # 从models导入数据库模型(数据库要创建的表) migrate = Migrate(app, db) # 将数据库和app关联起来 import viewsmodel.views db.init_app(app) # db和app # 让flaskapp支持命令行工作 manager = Manager(app) # 添加迁移脚本的命令到manager中 manager.add_command('db', MigrateCommand) # db.create_all() # 自定义命令 python 文件 runserver @manager.command def runserver(): app.run(debug=True, host='127.0.0.1', port=8888, threaded=True) # 此处 threaded=True 为多线程启动