API

flask_via

class flask_via.RoutesImporter[source]

Bases: object

Handles the import of routes module and obtaining a list of routes from that module as well as loading each route onto the application

include(routes_module, routes_name)[source]

Imports a routes module and gets the routes from within that module and returns them.

Parameters:
  • routes_module (str) – Python dotted path to routes module
  • routes_name (str) – Module attribute name to use when attempted to get the routes
Returns:

List of routes in the module

Return type:

list

load(app, routes, **kwargs)[source]

Loads passed routes onto the application by calling each routes add_to_app method which must be implemented by the route class.

class flask_via.Via[source]

Bases: flask_via.RoutesImporter

The core class which kicks off the whole registration processes.

Example

from flask import Flask
from flask.ext.via import Via
from flask.ext.via.routers.flask import Basic

app = Flask(__name__)

def foo(bar=None):
    return 'Foo View!'

routes = [
    Basic('/foo', foo),
    Basic('/foo/<bar>', foo, endpoint='foo2'),
]

via = Via()
via.init_app(app, routes_module='path.to.here')

if __name__ == "__main__":
    app.run(debug=True)
init_app(app, routes_module=None, routes_name='routes', **kwargs)[source]

Initialises Flask extension. Bootstraps the automatic route registration process.

Parameters:

app (flask.app.Flask) – Flask application instance

Keyword Arguments:
 
  • route_module (str, optional) – Python dotted path to where routes are defined, defaults to None
  • routes_name (str, optional) – Within the routes module look for a variable of this name, defaults to routes
  • **kwargs – Arbitrary keyword arguments passed to add_url_rule
Raises:
  • ImportError – If the route module cannot be imported
  • AttributeError – If routes do not exist in the moduke
  • NotImplementedError – If VIA_ROUTE_MODULE is not configured in appluication config and route_module keyword argument has not been provided.

flask_via.routers

Base router classes and utilities.

class flask_via.routers.BaseRouter[source]

Bases: object

Base router class all routers should inherit from providing common router functionality.

Example

from flask.ext.via.routers import BaseRouter

class MyRouter(BaseRouter):

    def __init__(self, arg):
        ...

    def add_to_app(self, app):
        ...
__init__()[source]

Constructor should be overridden to accept specific arguments for the router.

Raises:NotImplementedError – If method not implemented
add_to_app()[source]

Method all routers require, which handles adding the route to the application instance.

Raises:NotImplementedError – If method not implemented
class flask_via.routers.Include(routes_module, routes_name='routes')[source]

Bases: flask_via.routers.BaseRouter, flask_via.RoutesImporter

Adds the ability to include routes from other modules, this can be handy when you want to break out your routes into separate files for sanity.

Note

This is not a implementation of Flask blueprints

__init__(routes_module, routes_name='routes')[source]

Constructor for Include router, taking the passed arguments and storing them on the instance.

Parameters:routes_module (str) – Python dotted path to the routes module
Keyword Arguments:
 routes_name (str (optional)) – Name of the variable holding the routes in the module, defaults to routes
add_to_app(app, **kwargs)[source]

Instead of adding a route to the flask application this will include and load routes similar, same as in the flask_via.Via class.abs

Parameters:
  • app (flask.app.Flask) – Flask application instance
  • **kwargs – Arbitrary keyword arguments passed in to init_app

flask_via.routers.flask

A set of flask specific router classes to be used when defining routes.

Example

from flask.ext.via.routes.flask import Basic, Pluggable
from yourapp.views import BarView, foo_view

routes = [
    Basic('/foo', 'foo', foo_view),
    Pluggable('/bar', view_func=BarView.as_view('bar')),
]
class flask_via.routers.default.Basic(url, func, endpoint=None)[source]

Bases: flask_via.routers.BaseRouter

A basic Flask router, used for the most basic form of flask routes, namely functionally based views which would normally use the @route decorator.

Example

from flask.ext.via.routes import flask
from yourapp.views import foo_view, bar_view

routes = [
    Basic('/foo', 'foo', foo_view),
    Basic('/bar', 'bar', bar_view),
]
__init__(url, func, endpoint=None)[source]

Basic router constructor, stores passed arguments on the instance.

Parameters:
  • url (str) – The url to use for the route
  • func (function) – The view function to connect the route with
Keyword Arguments:
 

endpoint (str, optional) – Optional endpoint string, by default flask will use the view function name as the endpoint name, use this argument to change the endpoint name.

add_to_app(app, **kwargs)[source]

Adds the url route to the flask application object.mro

Parameters:
  • app (flask.app.Flask) – Flask application instance
  • **kwargs – Arbitrary keyword arguments passed in to init_app
class flask_via.routers.default.Blueprint(name, module, routes_module_name='routes', routes_name='routes', static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None)[source]

Bases: flask_via.routers.BaseRouter, flask_via.RoutesImporter

Registers a flask blueprint and registers routes to that blueprint, similar to flask_via.routes.Include.

Example

from flask.ext.via.routers import default

routes = [
    default.blueprint('foo', 'flask_via.examples.blueprints.foo')
]
__init__(name, module, routes_module_name='routes', routes_name='routes', static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None)[source]

Constructor for blueprint router.

Parameters:
  • name (str) – Blueprint name
  • module (str) – Python dotted path to the blueprint module, not the routes module
Keyword Arguments:
 
  • routes_module_name (str, optional) – The module Flask-Via will look for within the blueprint module which contains the routes, defaults to routes
  • routes_name (str, optional) – Name of the variable holding the routes in the module, defaults to routes
  • static_folder (str, optional) – Path to static files for blueprint, defaults to None
  • static_url_path (str, optional) – URL path for blueprint static files, defaults to None
  • template_folder (str, optional) – Templates folder name, defaults to None
  • url_prefix (str, optional) – URL prefix for routes served within the blueprint, defaults to None
  • subdomain (str, optional) – Sub domain for blueprint, defaults to None
  • url_defaults (function, optional) – Callback function for URL defaults for this blueprint. It’s called with the endpoint and values and should update the values passed in place, defaults to None.
add_to_app(app, **kwargs)[source]

Creates a Flask blueprint and registers routes with that blueprint, this means any routes defined will be added to the blueprint rather than the application.

Parameters:
  • app (flask.app.Flask) – Flask application instance
  • **kwargs – Arbitrary keyword arguments passed in to init_app
create_blueprint()[source]

Creates a flask blueprint instance.

routes_module[source]

Generates the routes module path, this is built from self.module and self.routes_module_name.

Returns:Python dotted path to the routes module containing routes.
Return type:str
class flask_via.routers.default.Pluggable(url, **kwargs)[source]

Bases: flask_via.routers.BaseRouter

Pluggable View router class, allows Flask pluggable view routes to be added to the flask application.

Example

from flask.ext.via.routers import flask
from flask.views import MethodView

class FooView(MethodView):
    def get(self):
        return 'foo view'

class BarView(MethodView):
    def get(self):
        return 'bar view'

routes = [
    flask.Pluggable('/', view_func=FooView.as_view('foo'))
    flask.Pluggable('/', view_func=BarView.as_view('bar'))
]
__init__(url, **kwargs)[source]

Pluggable router constructor, stores passed arguments on instance.

Parameters:
  • url (str) – The url to use for the route
  • **kwargs – Arbitrary keyword arguments passed to add_url_rule
add_to_app(app, **kwargs)[source]

Adds the url route to the flask application object.

Parameters:
  • app (flask.app.Flask) – Flask application instance
  • **kwargs – Arbitrary keyword arguments passed in to init_app

flask_via.routers.restful

Routers for the Flask-Restful framework.

class flask_via.routers.restful.Resource(url, resource, endpoint=None)[source]

Bases: flask_via.routers.BaseRouter

The Resource router allows you to define Flask-Restful routes and have those API resources added to the application automatically. For this to work you must at init_app time pass a optional keyword argument restful_api to init_app with its value being the restful api extension instance.

Example

app = Flask(__name__)
api = restful.Api(app)

class FooResource(restful.Resource):

    def get(self):
        return {'hello': 'world'}

routes = [
    Resource('/foo', FooResource)
]

via = Via()
via.init_app(
    app,
    routes_module='flask_via.examples.restful',
    restful_api=api)

if __name__ == '__main__':
    app.run(debug=True)
__init__(url, resource, endpoint=None)[source]

Constructor for flask restful resource router.

Parameters:
  • url (str) – The url to use for the route
  • resource – A flask restful.Resource resource class
Keyword Arguments:
 

endpoint (str, optional) – Optional, override Flask-Restful automatic endpoint naming

add_to_app(app, restful_api=None)[source]

Adds the restul api resource route to the application.

Parameters:app (flask.app.Flask) – Flask application instance, this is ignored.
Keyword Arguments:
 restful_api (str) – Instantiated restful API instace, used to add the route.
Raises:NotImplementedError – If restful_api is not provided

http://thisissoon.com

Table Of Contents

Related Topics

This Page