0

in my flask app app/__init__.py I have included the below function

def create_app(config_filename=None):
    _app = Flask(__name__, instance_relative_config=True)
    with _app.app_context():
        print "Creating web app",current_app.name
        cors = CORS(_app, resources={r'/*': {"origins": "*"}})
        sys.path.append(_app.instance_path)
        _app.config.from_pyfile(config_filename)
        from config import app_config
        config = app_config[_os.environ.get('APP_SETTINGS', app_config.development)]
        _app.config.from_object(config)
        register_blueprints(_app)
        # _app.app_context().push()
        return _app

now I also have /app/datastore/core.py in which I have

import peewee
import os

from flask import g, current_app
cfg = current_app.config    
dbName = 'clinic_backend'


def connect_db():
    """Connects to the specific database."""
    return peewee.MySQLDatabase(dbName, 
    user=cfg['DB_USER'], 
    host=cfg['DB_HOST'], 
    port=3306, 
    password=cfg['DB_PWD']
    )

def get_db():
    """ Opens a new database connection if there is none yet for the
    current application context.
    """
    if not hasattr(g, 'db'):
        g.db = connect_db()
    return g.db

when I create my run my app start.py it creates the app object but when I try access the URL in browser I get error saying

 File "/Users/ciasto/Development/python/backend/app/datastore/core.py",
 line 31, in get_db
     g.db = connect_db()   File "/Users/ciasto/Development/python/backend/venv/lib/python2.7/site-packages/werkzeug/local.py",
 line 364, in <lambda>
     __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)   File
 "/Users/ciasto/Development/python/backend/venv/lib/python2.7/site-packages/werkzeug/local.py",
 line 306, in _get_current_object
     return self.__local()   File "/Users/ciasto/Development/python/backend/venv/lib/python2.7/site-packages/flask/globals.py",
line 44, in _lookup_app_object
    raise RuntimeError(_app_ctx_err_msg) 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.

what am I doing wrong here?

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • Have you looked at how others have connected to the Peewee database using Flask? This Q&A looks like it might help: https://stackoverflow.com/questions/45782020/peewee-and-flask-database-object-has-no-attribute-commit-select – Hugo Jan 03 '19 at 12:38
  • yes off course, infact I was doing that way earlier; but my web app is running 24/7 and after a gap of 12 hours (mostly after night ) in morning I will notice that my flask app not able to fetch and giving error MySQL Server has gone away so I looked into this thread: https://stackoverflow.com/q/34038185/3311276 and what I have posted in my this OP is what is demonstrated in http://flask.pocoo.org/docs/dev/tutorial/dbcon/ (the highest posted answer). I have learned that g is a localProxy object but I do not know how to solve this issue. – Ciasto piekarz Jan 03 '19 at 12:58
  • You need to just connect when a request starts and disconnect from the db when a request finishes. Flask comes with signal handlers/hooks for registering callbacks that will take care of it for you, or you can use http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#database-wrapper – coleifer Jan 08 '19 at 22:36

1 Answers1

0

what am I doing wrong here?

Just about everything.

Peewee already exposes database connections as a threadlocal so what you're doing is senseless. Especially given from reading your comments you are only trying to add connection hooks.

The peewee docs are QUITE CLEAR: http://docs.peewee-orm.com/en/latest/peewee/database.html#flask

As an aside, read the damn docs. How many questions have you posted already that could have been easily answered just by reading the documentation?

coleifer
  • 24,887
  • 6
  • 60
  • 75
  • What is written in docs is exactly what I am doing(having read the docs) perhaps it was my bad I forgot to update the post. I have also learnt about ` flask.g` where I was actually having the problem and you jumped into something else. Thanks – Ciasto piekarz Jan 09 '19 at 04:01