title: access app config variables in Flask app
body: <p>I have a flask app that looks like this:</p>
  
  <pre><code>myapp
  -- application.py
  -- models.py
  -- queries.py
  -- routes.py
  -- settings.py
  </code></pre>
  
  <p>application.py looks like this:</p>
  
  <pre><code>from flask import Flask
  from myapp import routes
  
  def create_app(config_object):
  
      app = Flask(__name__)
      app.config.from_object(config_object)
  
      app.register_blueprint(routes.main)
  
      return app
  </code></pre>
  
  <p>queries.py looks like this</p>
  
  <pre><code>from myapp.models import User
  
  class QueryConnection(DATABASE_URI):
      self.engine = create_engine(DATABASE_URI)
      self.session = sessionmaker(bind=self.engine)
      self.Session = self.session()
  
      def get_all_users(self):
          return self.Session.query(User).all()
  </code></pre>
  
  <p>routes.py looks like this:</p>
  
  <pre><code>from flask import current_app, Blueprint, render_template
  from myapp import queries
  main = Blueprint('main', __name__, url_prefix='/')
  
  query_connection = queries.QueryConnection(current_app.config['DATABASE_URI'])
  
  @main.route("/")
  def index():
      return render_template('index.jinja2', list=query_connection.get_all_users())
  </code></pre>
  
  <p>models.py looks like this:</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, Integer, String
  
  Base = declarative_base()
  
  class User(Base):
      __tablename__ = 'users'
  
      id           = Column(Integer, primary_key=True)
      name         = Column(String)
  </code></pre>
  
  <p>I want to get the current_app.config['DATABASE_URI'] in routes.py so I can query the database but I don't know how to pass the app context from application.py to queries.py. I tried using this in application.py</p>
  
  <pre><code>with app.app_context():
      app.register_blueprint(main)
  </code></pre>
  
  <p>But I get the error "working outside of application context". Anything else gives the same error.</p>
  
  <p>I want to encapsulate the queries in its own class so that I can pass the database uri in different contexts like in tests.</p>