49

app/init.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__ name __)
db = SQLAlchemy(app)  
from app import views, models

app/models.py:

from app import db  # I want to avoid this everywhere

I really don't like my submodules having a dependency on their parent. Also can the global package variables be avoided too? I want a more OO solution.

One alternative for app is to use Blueprints I think, but then I loose the route decorator. Also the same cannot be done for db with SQLAlchemy (or can it?).

1
  • refer this link for a great guide towards creating project structure for flask.
    – code_10
    Nov 20, 2022 at 13:21

2 Answers 2

71

Take a look at this project: https://github.com/sloria/cookiecutter-flask
It's a great example for doing things the right way. Many of great Flask features are used: blueprints, application factories and more.

Here is how they register extensions, such as SQLAlchemy Database:

# app/extensions.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
...


# app/app.py
from app.extensions import db

def create_app(config_object=ProdConfig):
    app = Flask(__name__.split('.')[0])
    app.config.from_object(config_object)
    register_extensions(app)
    ...

def register_extensions(app):
    db.init_app(app)
    ...
4
  • 1
    Great, I think this is exactly what I was looking for. So basically defining these globals in another package gets rid of the need for a circular import? That really cleans up the mess.
    – ovg
    Mar 20, 2017 at 18:33
  • 1
    Exactly. Every object has its place and it's all neatly working together.
    – leovp
    Mar 20, 2017 at 18:35
  • 6
    It took me an embarrassingly long time to get Flask setup the way I wanted. This helped tremendously. Many thanks. Jun 14, 2018 at 3:05
  • Guess it's the best practice Sep 7, 2020 at 20:25
31

Try use 3rd. We create exts.py file to instancing SQLAlchemy like this:

exts.py

from flask_sqlalchemy import SQLAlchemy
from flask_xxx import xxx

db = SQLAlchemy()
...

run.py

from flask import Flask

from .exts import db, ...


def register_extensions(app):
    db.init_app(app) 
    ... 


def create_app(config):
    app = Flask(__ name __)
    app.config.from_object(config)

    register_extensions(app)

    return app

app = create_app(config)

models.py

from .exts import db


class XXXModel(db.Model):
    pass    
4
  • This answer is more complete in the code example than the accepted one. Crucial addition is that app is defined in the called python file and given the return value of create_app()
    – CodeMantle
    Apr 4, 2020 at 15:57
  • 2
    But where to write db.create_all(). May 12, 2021 at 16:03
  • A flask project generator. Based on Flask + SQLAlchemy + marshmallow + webargs. github.com/TTWShell/hobbit-core Jun 27, 2021 at 0:46
  • @ThunderPhoenix try Flask-Migrate. Jun 27, 2021 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.