title: Getting the database connection from application context with Flask-Restful
body: <p>I have an app using Flask-Restful and I don't know how to get the database connection info from the application context.</p>
  
  <p>Here is what I have so far:</p>
  
  <p><strong>app.py:</strong></p>
  
  <pre><code>....
  from flask.ext.restful import reqparse, abort, Api, Resource
  app = Flask(__name__)
  app.config['DATABASE'] = 'my.db'
  api = Api(app) 
  api.add_resource(Foo, '/')
  </code></pre>
  
  <p><strong>foo.py</strong></p>
  
  <pre><code>...
  from flask.ext.restful import Resource
  
  class Foo(Resource):
      def __init__(self):
          self.db = get_db()
      def post(self):
          do_stuff_with_db()
  </code></pre>
  
  <p>I'd like the get_db method to look something like:</p>
  
  <pre><code>def get_db():
      return database.connect(app.config['DATABASE'])
  </code></pre>
  
  <p>The problem is in order to get app.config into foo.py, I create a circular reference. Is there a clean, and hopefully idomatic way to get this?</p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/21707589/python-flask-restful-cannot-get-app-access-in-resource-class?rq=1">this question</a>, but the solution given doesn't actually resolve the circular import problem.</p>