Flask, SQLAlchemy and Celery integration tool.
Under development. Visit https://github.com/mtth/flasker for the latest version.
What Flasker is!
- A transparent integration of Flask, SQLAlchemy and Celery which lets you configure these individually according to your project needs via a single .cfg file.
- A simple pattern to organize your project via the current_project proxy (cf. Structuring your project for an example). No more complicated import schemes!
- A command line tool from where you can create new projects, launch the Flask buit in Werkzeug server, start Celery workers and the Flower tool, and run a shell in the current project context (inspired by Flask-Script).
What Flasker isn’t?
- A simplified version of Flask, Celery, and SQLAlchemy. Flasker handles the setup but intentionally leaves you free to interact with the raw Flask, Celery and SQLAlchemy session objects. Some knowledge of these frameworks is therefore required.
Flasker also comes with two optional extensions:
$ pip install flasker
To create a new project:
$ flasker new basic
This will create a project configuration file default.cfg in the current directory and a basic Bootstrap themed app (this can be turned off with the -a flag).
$ flasker -h
This will list all commands now available for that project:
- server to run the app server
- worker to start a worker for the Celery backend
- flower to run the flower worker management app
- shell to start a shell in the current project context (useful for debugging)
- new to create a new default configuration file
Extra help is available for each command by typing:
$ flasker <command> -h
Structuring your project
Here is a sample minimalistic project configuration file:
[PROJECT] NAME = My Project MODULES = app.views, app.tasks DB_URL = sqlite:///db/db.sqlite [APP] # any valid Flask configuration option can go here # cf http://flask.pocoo.org/docs/config for the full list DEBUG = True TESTING = True [CELERY] # any valid Celery configuration option can go here # cf http://docs.celeryproject.org/en/latest/configuration.html BROKER_URL = redis://
When it starts, the flasker command line tool imports all the modules declared in the MODULES key of the configuration file (in the PROJECT section). Inside each of these you can use the current_project proxy to get access to the Flask application object, the Celery application object and the SQLAlchemy database sessions. Therefore a very simple pattern inside each module is to do:
from flask import render_template from flasker import current_project # the Flask application app = current_project.app # the Celery application celery = current_project.celery # the SQLAlchemy scoped session maker is available on db.session db = current_project.db # normally you probably wouldn't need all three in a single file # but you get the idea :). and now you can do stuff with each... @app.route('/') def index(): """A random view.""" return render_template('index.html') @celery.task def task(): """And a great task.""" pass # and so on...
Once Flasker has finished importing all your project module files and configuring the applications, it handles startup.
Cf. the Wiki for all the available configuration options.
This extension is meant to very simply expose URL endpoints for your models.
There exist other great ReSTful extensions for Flask. Here are the main differences with two popular ones:
FlaskRESTful works at a sligthly lower level. It provides great tools but it would still require work to tie them with each model. Here, the extension uses the Flasker model structure to do most of the work.
Flask-Restless is similar in that it also intends to bridge the gap between views and SQLAlchemy models. However the Flasker API is built to provide:
- Faster queries: the ‘jsonification’ of model entities is heavily optimized for large queries.
- More flexibility: API responses are not restricted to returning model columns but also return properties.
- Convenient access to nested models: queries can go arbitrarily deep within nested models (the extension takes care of not repeating information). This is especially useful with a client-side library such as Backbone-Relational.
- More endpoints: each one-to-many relation can have its own model specific endpoint.
- Support for models with composite primary keys
Nevertheless this extension is much younger and currently lacks several great features offered by Flask-Restless (such as arbitrary queries and function evaluation).
Here is a very simple sample file:
from flasker import current_project from flasker.ext.api import APIManager from flasker.util import Model from sqlalchemy import Column, ForeignKey, Integer, Unicode # Create the APIManager api_manager = APIManager(add_all_models=True) current_project.register_manager(api_manager) # Define the models class House(Model): id = Column(Integer, primary_key=True) address = Column(Unicode(128)) class Cat(Model): name = Column(Unicode(64), primary_key=True) house_id = Column(ForeignKey('houses.id')) house = relationship('House', backref='cats')
Which will create the following endpoints:
- /api/houses/ (GET, POST)
- /api/houses/<id> (GET, PUT, DELETE)
- /api/houses/<id>/cats/ (GET, PUT)
- /api/houses/<id>/cats/<position> (GET)
- /api/cats/ (GET, POST)
- /api/cats/<name> (GET, PUT, DELETE)
Cf. the Wiki for the complete list of available options.
Adding the following code to any one of your modules will allow you to restrict access to your application:
from flasker import current_project from flasker.ext.auth import GoogleAuthManager auth_manager = GoogleAuthManager( client_id='your_google_client_id', authorized_emails=['email@example.com', 'firstname.lastname@example.org', ...] ) current_project.register_manager(auth_manager)
Cf. the Wiki for the complete list of available options.
Available utilities include:
Cf. the Wiki for a more detailed explanation on some of the available utilities.