Mapper(blueprint, base_import_name=None, **url_defaults)¶
Provides a convenience wrapper around
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_endpointwill 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.
A wrapper around Flask to help make managing complex applications easier.
Why not use Flask’s
before_first_requestdecorator? 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
Flaskinstance. If none is provided the
HipPocket.init_appmethod 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.
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
taskdecorator during decoration.
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)
The task decorator does not handle single callable arguments. If you need to pass single callables pass it as a keyword argument.
# 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.
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.
Add a handler for each of the available HTTP error responses.
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