access app config variables in Flask app access app config variables in Flask app flask flask

access app config variables in Flask app


you need to import the file, from where you are calling the create_app method. My guess is that it is the application.py.

--- D_file.py

app = create_app(your_config)

--- queries.py

from D_file import appDB_URI = app.config['DATABASE_URI']


Why are you involving the Flask application at all in the SQLAlchemy setup? You have a configuration object available to you, use that directly. Then you can attach a session to the flask global object, or create a session dynamically.

In queries.py

import get_config from configconfig_object = get_config()engine = create_engine(config_object['DATABASE_URI'])session_factory = sessionmaker(bind=engine)Session = scoped_session(session_factory)def get_all_users():    session = Session()    users = session.query(User).all()    session.close()    return users

And in your app factory:

from flask import Flask, gfrom myapp import routesfrom myapp.queries import Sessiondef create_app(config_object):    app = Flask(__name__)    app.config.from_object(config_object)    app.register_blueprint(routes.main)    # automatically set up and close a session for each request    @app.before_request    def before_request(response):        g.session = Session()    @app.after_request    def after_request(response):        g.session.close()    return app