Sitemap
Mark Steve Samson

Mark Steve Samson

Web Apps & Open Source

My Flask Pattern

--

I’ve been using Flask for my recent projects. It’s an awesome library — unopinionated but still provides enough convenience and good defaults. You’ll only be using the functions shown in the tutorial and examples most of the time. Only when you need to do something specific will you need to check the API docs. But I suggest you browse through it anyways since there are quite a few stuff not mentioned in the user guide that are quite useful.

The best thing for me about Flask is that it’s more of a library than a framework. There’s no bootstrap script. You don’t need to follow a specific directory structure. You can write an app in a single module if that’s how you roll. I formed my conventions and preferences as I wrote more Flask apps. And now I’ve finally settled on a pattern after experimenting with different setups.

Get Mark Steve Samson’s stories in your inbox

Join Medium for free to get updates from this writer.

I start a project by creating a package the following dirs and modules:

project/
static/
templates/
__init__.py
config.py
extensions.py
views.py
wsgi.py

Let’s go through them one by one. I’ll be skipping static/, templates/ and __init__.py since they’re, well, empty.

config.py

It put my default settings here. I don’t put actual config values here since I include this in the VCS. It’s a good idea to document each setting too if you’re releasing it as an open source project.

extensions.py

Don’t reinvent the wheel. Extensions are available for common tasks such as db access, authentication and assets management.

from flask_sqlite3 import SQLite3
from flask_googlelogin import GoogleLogin
db = SQLite3()
googlelogin = GoogleLogin()

Note that I only create extension instances here. I set them up with the app later as you’ll see.

views.py

I don’t add routes to the main app directly. I use blueprints. Here’s an example:

from flask import Blueprintroot = Blueprint(‘root’, __name__)@root.route(‘/’)
def index():
return “Hello, world”

Now if I want to add another section to my app, say an admin section, I can promote views.py to a package. I do this with python-fu:

$ promote project.views

This will move views.py to views/__init__.py. I move the root blueprint code into its own module views/root.py and import it from views/__init__.py. I can also add, let’s say, an admin blueprint.

# __init__.py

from .root import root
from .admin import admin
# root.py

from flask import Blueprint
root = Blueprint(‘root’, __name__)@root.route(‘/’)
def index():
return “Hello, world”
# admin.py

from flask import Blueprint
admin = Blueprint(‘root’, __name__)@admin.route(‘/’)
def index():
return “Admin”

Notice I still haven’t associated the config and routes to the app itself. I haven’t even defined the app yet! Time for the last module.

wsgi.py

I chose to name it like this because most WSGI servers use this convention. This is where I put the main Flask app, set up extensions and register the blueprints.

from flask import Flaskfrom .extensions import db, googlelogin
from .views import root, admin
app = Flask(__name__, instance_relative_config=True)
app.config.from_object(‘project.config’)
app.config.from_pyfile(‘application.cfg’, silent=True)
db.init_app(app)
googlelogin.init_app(app)
app.register_blueprint(root)
app.register_blueprint(admin, url_prefix=’/admin’)
application = app

You must have noticed I toggled instance_relative_config to True. This is my preferred way of handling configuration files. You can know more about how instance folder works in the docs.

There are other modules you might want to add like models.py and helpers.py. Also, aside from the package modules, it’s a good idea to have requirements.txt and/or setup.py to handle your dependencies. Finally, for local testing I usually have a runserver.py script.

if __name__ == ‘__main__’:
from project.wsgi import app
app.run(debug=True)

So that’s how I set up my Flask projects. Next time I’ll write about how I manage and serve WSGI apps with Circus and Nginx.

--

--