def session_scope(self):
        """Context manager to use session.
        If `setup_session()` is not called beforehand,
        current config will be used to build session.
        """
        if self.Session is None:
            config = get_config()
            self.setup_session(config)

        session = self.Session()
        try:
            yield session
            session.commit()
        except:
            session.rollback()
            raise
        finally:
            session.close()
Exemple #2
0
    def _load_model(cls):
        if cls.__model is None:
            config = get_config()
            model_path = os.path.join(
                ROOT_DIR, config.ML_MODELS['DOG_BREED']['MODEL_DATA'])

            interpreter = tflite.Interpreter(model_path=model_path)
            interpreter.allocate_tensors()

            input_details = interpreter.get_input_details()
            input_index = input_details[0]['index']
            output_details = interpreter.get_output_details()
            output_index = output_details[0]['index']

            cls.__model = {
                'interpreter': interpreter,
                'input_index': input_index,
                'output_index': output_index
            }
def create_app(mode: str) -> Flask:
    """Create flask app."""
    try:
        set_config(mode)
    except ConfigAlreadySetError:
        # if already set config, reuse it
        pass

    config = get_config()

    app = Flask(__name__,
                static_folder=config.STATIC_DIR,
                template_folder=config.STATIC_DIR)

    if not os.path.exists(
            os.path.join(ROOT_DIR, config.STATIC_DIR, config.UPLOAD_DIR)):
        os.makedirs(
            os.path.join(ROOT_DIR, config.STATIC_DIR, config.UPLOAD_DIR))
    app.config.from_object(config)
    app.url_map.converters['regex'] = RegexConverter

    setup_session(config)

    from image_app.web import base as base_bp
    app.register_blueprint(base_bp)

    from image_app.web.api import api as api_bp
    app.register_blueprint(api_bp)

    app.wsgi_app = WhiteNoise(app.wsgi_app, root=config.STATIC_DIR)
    app.wsgi_app.add_files(config.STATIC_DIR)

    if config.DEBUG:
        with app.app_context():
            init_db()

    @app.teardown_appcontext
    def shutdown_session(exception=None):
        session_removal()

    return app
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os.path

from flask import render_template, send_from_directory

from . import base

from image_app.settings import get_config

config = get_config()


@base.route('/', defaults={'path': ''})
@base.route('/<path:path>')
def index(path):  # pragma: no cover
    return render_template('index.html')


@base.route(r"/static/<regex('(.*?)\.(jpg|png|ico|js|json|txt)$'):file>",
            methods=["GET"])
def public(file):  # pragma: no cover
    # access public directory in frontend app
    return send_from_directory(config.STATIC_DIR, file)
 def get_session(self):
     if self.Session is None:
         config = get_config()
         self.setup_session(config)
     return self.Session()
    @app.teardown_appcontext
    def shutdown_session(exception=None):
        session_removal()

    return app


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('-m',
                        '--mode',
                        type=str,
                        default='dev',
                        help='Configuration type')

    args = parser.parse_args()

    mode = args.mode
    if mode == 'production':
        warnings.warn('Production mode should not be run from `app.py`')

    elif mode not in ('test', 'dev'):
        mode = 'dev'

    from image_app.settings import set_config, get_config
    set_config(mode)

    app = create_app(get_config())
    app.run()
Exemple #7
0
 def setUpClass(cls):
     cls.app = create_app('test')
     cls.app_context = cls.app.app_context()
     cls.app_context.push()
     setup_session(get_config())