How to access flask's app.config or current_app.config?

Viewed 398

Problem

Below is the project structure codes. Using the 'from flask import current app' the current_app is accessible to the routes.py but not on the others and I am getting the following error:

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

As it turned out printing dir(current_app) on these error producing py files returns '[]' but not on the routes.py. How can I access the value of current_app.config['ENGINE'] for use on the models.py?

Project Structure

project
|---->main
|      |---->somefunction
|      |          |----forms.py
|      |          |----routes.py
|      |
|      |----__init__.py
|      |----config.py
|      |----models.py
|      
|----run.py

run.py (not sure I am using the app.app_context().push() correctly here)

from main import create_app
from main.config import Dev, Pro, Net, Rem

app = create_app(Dev)
app.app_context().push()


if __name__ == "__main__":
    app.run()

init.py (dir(current_app) returns '[]' on this file)##

from otherpackages import *
from main.config import Dev

def create_app(config_class=Dev):
    app = Flask(__name__)
    app.config.from_object(config_class)

    other codes (register routes)

    return app

config.py (dir(current_app) returns '[]' on this file)

class Cfg(object):
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_TRACK_MODIFICATIONS = False
PIPENV_VENV_IN_PROJECT = True

class Dev(Cfg):
    ENV = 'development'
    DEBUG = True
    TEMPLATES_AUTO_RELOAD = True
    ENGINE = engLoc

class Pro(Cfg):
    ENV = 'production'
    DEBUG = False
    TEMPLATES_AUTO_RELOAD = False
    ENGINE = engLoc

class Net(Pro):
    ENGINE = engNet

class Rem(Pro):
    ENGINE = engRem

models.py (dir(current_app) returns '[]' on this file)

from otherpackages import *
from flask import current_app

engine = current_app.config['ENGINE']

def dbReflect(eng):
    somecode
    return

dbReflect(engine)

sess = Session(bind=engine)
0 Answers
Related