API

URL Mapping

class flask.ext.hippocket.Mapper(blueprint, base_import_name=None, **url_defaults)

Provides a convenience wrapper around LateLoader.

It is designed to be used as follows:

from flask import Blueprint
from flask.ext.hippocket import Mapper

routes = Blueprint("my_blueprint", __name__, url_prefix="/my-blueprint")
mapper = Mapper(routes, "apps_package.my_app")

mapper.add_url_rule("/", "my_module.my_endpoint")

apps_package.my_app.my_module.my_endpoint will be imported when the URL /my-blueprint/ is hit for the first time. This import will be cached in order to ensure that subsequent requests to this url will not result in additional imports.

add(url, import_name, **url_kwargs)

add is an alias for add_url_rule.

add_url_rule(url, import_name, **url_kwargs)

Add a URL to the wrapped Flask or Blueprint‘s URL map.

Task Runner

class flask.ext.hippocket.HipPocket(app=None, tasks=None)

A wrapper around Flask to help make managing complex applications easier.

Why not use Flask’s before_first_request decorator? Because functions registered via that decorator are run on the first request (as opposed to before it).

This extension fills the gap between Flask-Script and before_first_request. It is for attaching functionality that needs to be run on the application itself (as opposed to Flask-Script which is used to run jobs with the application) but which might be too intensive to run in response to the first request.

Alternately, because a flask is always better when you have something to carry it in.

Parameters:app – A Flask instance. If none is provided the HipPocket.init_app method may be called later

to initialize the Flask instance.

Parameters:tasks – An iterable of callable objects. Each callable will be passed the app in turn.
init_app(app)

Runs each of the tasks added via the task decorator.

task(func, *args, **kwargs)

A task is a unit of work to be performed by HipPocket on an initialized Flask app.

All tasks must take the initialized app as their first argument. They can take any other arguments at all - these arguments must be provided to the task decorator during decoration.

Examples:

from flask import Flask
from flask.ext.hippocket import HipPocket

hip_pocket = HipPocket()

@hip_pocket.task
def do_something(app):
    # Do something with app

@hip_pocket.task(an_arg, another_arg)
def do_something_else(app, an_arg, another_arg):
    # Do something with app, an_arg and another_arg

app = Flask(__name__)

hip_pocket.init_app(app)

Note

The task decorator does not handle single callable arguments. If you need to pass single callables pass it as a keyword argument.

An example:

# This will fail
@hip_pocket.task(lambda x: x*2)
def schedule_doubles(app, doubler):
    # Have the manager schedule something.

# This will work as anticipated
@hip_pocket.task(butler=lambda garment: garment.add_pocket_squares())
def add_pocket_squares(app, butler):
    # Add pocket squares to the appropriate garments.

Tasks

flask.ext.hippocket.tasks.autoload(app, apps_package='apps', module_name='routes', blueprint_name='routes', on_error=None)

Automatically load Blueprints from the specified package and registers them with Flask.

flask.ext.hippocket.tasks.setup_errors(app, error_template='errors.html')

Add a handler for each of the available HTTP error responses.

Helpers

class flask.ext.hippocket.pocket.LateLoader(import_name)

Provides a way of loading views on the first request for the view rather than on application initialization. This speeds up application load times and keeps resource requirements low (since you don’t load code until you need it).

Taken directly from the example in Flask’s docs