def create_app(test_config=None): myApp = Flask(__name__, instance_relative_config=True) # SECRET_KEY is to keep nasty hackers from doing dum stuff. # DATABASE is path of our database myApp.config.from_mapping(SECRET_KEY="superSecretGlobalKey", DATABASE=os.path.join(myApp.instance_path, "CarDB.sqlite")) if test_config is None: # If there is a configured instance, load it. config.py can store values # you don't want visible, like a real secret key. myApp.config.from_pyfile("config.py", silent=False) else: # otherwise, test configuration myApp.config.from_mapping(test_config) try: os.makedirs(myApp.instance_path) except OSError: pass #import blueprints here from app import database from app import index # register blueprints here. myApp.register_blueprint(index.bp) database.init_app(myApp) return myApp
def create_app(config_name): # app app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) # database db.init_app(app) db.app = app migrate.init_app(app, db) # cache from app.cache import CachingConfig, cache app.config.from_object(CachingConfig) cache.init_app(app) # 国际化 babel.init_app(app) # flask-admin admin.init_app(app) admin.name = app.config.get("APP_NAME") # flask-user from app.models import User from app.flask_user_custom import FlaskUserConfig app.config.from_object(FlaskUserConfig) # 添加 FlaskUserConfig 配置 app.config["USER_APP_NAME"] = app.config.get("APP_NAME") user_manager.init_app(app, db, User) # toolbar # toolbar.init_app(app) # jinja2 env from app.jinja2_env import init_jinja2_env init_jinja2_env(app) # views from app.views import views app.register_blueprint(views) # error pages # from app.errors import init_errors_page # init_errors_page(app) amdin_index_view.init_errors_page(app) # apis from app.apis import api app.register_blueprint(api, url_prefix="/api/v1_0") from app.flask_restless_custom import api_manager api_manager.init_app(app, flask_sqlalchemy_db=db) api_manager.app = app # 慢查询日志 from app.database import init_app init_app(app) return app
def client(): db_fd, application.config['DATABASE'] = tempfile.mkstemp() application.config['TESTING'] = True application.config['DEBUG'] = False with application.test_client() as client: with application.app_context(): database.init_app(application) yield client os.close(db_fd) os.unlink(application.config['DATABASE'])
def client(): file = tempfile.mkstemp() db, app.config["SQLALCHEMY_DATABASE_URI"] = file[ 0], "sqlite:///" + file[1] + ".db" app.config["TESTING"] = True with app.test_client() as client: with app.app_context(): database.init_app(app) database.create_all() database.session.commit() yield client os.close(db)
def create_app(): """Factory function that creates the Flask app""" app = Flask(__name__) app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') logging.basicConfig(level=logging.DEBUG) @app.route('/') def home(): """Our only non-Dash route, to demonstrate that Flask can be used normally""" return render_template('index.html') # Initialize extensions database.init_app(app) # PostgreSQL db with psycopg2 # For the Dash app register_dashapps(app) return app
def create_app(test_config=None): # create and config the app app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', SQLALCHEMY_DATABASE_URI='sqlite:///' + os.path.join(app.instance_path, 'iaa.db'), SQLALCHEMY_TRACK_MODIFICATIONS=False, ) if not test_config: # load the instance config, if exists, when not testing app.config.from_pyfile('dev_config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # initialize app with database from app.database import init_app init_app(app) # a simple page that test api @app.route('/') def test(): return app.send_static_file('index.html') # register blueprints from app import api app.register_blueprint(api.bp) # 解决跨域问题 app.after_request(cors) return app
def create_app(instance_name): app = Flask(APP_NAME, instance_relative_config=True) # app.config.from_json(f'{instance_name}.json', silent=True) app.config.from_json('local.json', silent=True) app.config.from_mapping(environ) app.config['FLASK_ENV'] = instance_name with app.app_context(): from app import models from app import endpoints from app import schemas from app import migrations from app import database models.init_app(app) endpoints.init_app(app) schemas.init_app(app) migrations.init_app(app) database.init_app(app) return app
def authenticated_client(): file = tempfile.mkstemp() db, app.config["SQLALCHEMY_DATABASE_URI"] = file[ 0], "sqlite:///" + file[1] + ".db" app.config["TESTING"] = True with app.test_client() as client: with app.app_context(): database.init_app(app) database.create_all() database.session.commit() login.init_app(app) test_user = User(username="******", email="*****@*****.**") test_user.set_password("admin") database.session.add(test_user) database.session.commit() test_todo_1 = Todo(task="Cooking", owner=test_user) test_todo_2 = Todo(task="Playing", owner=test_user) database.session.add(test_todo_1) database.session.add(test_todo_2) database.session.commit() yield client os.close(db)
def client(): file = tempfile.mkstemp() db, app.config["SQLALCHEMY_DATABASE_URI"] = file[ 0], "sqlite:///" + file[1] + ".db" app.config["TESTING"] = True with app.test_client() as client: with app.app_context(): database.init_app(app) database.create_all() database.session.commit() login.init_app(app) test_user = User(username="******", email="*****@*****.**") test_user.set_password("admin") database.session.add(test_user) database.session.commit() @login.user_loader def load_user(user_id): return User.query.get(int(user_id)) yield client os.close(db)
def create_app(test_config=None): """ Create and configure an instance of the Flask application. Some config variable we need to specify in 'instance/config.py' DEBUG SECRET_KEY GITHUB_SECRET REPO_PATH """ dictConfig({ 'version': 1, 'formatters': { 'default': { 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', } }, 'handlers': { 'dc_handler': { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'INFO', 'filename': '/var/log/flask/flask.log', 'formatter': 'default', 'mode': 'a', 'maxBytes': 10000, 'backupCount': 1 }, }, 'loggers': { 'flask.app': { 'handlers': ['dc_handler'] } }, }) flask = Flask(__name__, instance_relative_config=True) # ensure the instance folder exists try: os.makedirs(flask.instance_path) except OSError: pass # a default secret that should be overridden by instance config flask.config.from_mapping(SECRET_KEY='this-is-a-secret-key') if test_config is None: if flask.config['ENV'] == 'development': flask.config.from_object('instance.config.DevelConfig') else: flask.config.from_object('instance.config.ProductionConfig') else: # load the test config if passed in flask.config.update(test_config) # register the database commands from app import database database.init_app(flask) # apply the blueprints to the app from app import main, user, wizard, webhook, online_help flask.register_blueprint(main.bp, url_prefix='/dc') flask.register_blueprint(user.bp, url_prefix='/dc') flask.register_blueprint(wizard.bp, url_prefix='/dc') flask.register_blueprint(webhook.bp, url_prefix='/dc') flask.register_blueprint(online_help.bp, url_prefix='/dc') return flask
import json from flask import Flask, Response, abort, request from app import app from app.utils import (JSON_MIME_TYPE, search_pokedexes_by_name, get_paginated_list, crossdomain) from app.database import init_app pokedexes = init_app() @app.route('/api/v1/pokedexes') @crossdomain(origin='*') def list_pokedexes(): """ List all pokedexes with pagination. Can filter the list by name with args is query. """ query = request.args.get('query', None) results = pokedexes url = request.host_url + 'api/v1/pokedexes' if query: results = search_pokedexes_by_name(pokedexes, query) pokedexes_paginated = get_paginated_list( results, url, offset=request.args.get('offset', 1), limit=request.args.get('limit', 20),