Routers

Here you will find the documentation for each bundled router provided by Flask-Via.

Flask Routers

These routers are designed to work with standard flask functional and class based pluggable views.

Basic Router

The flask_via.routers.default.Basic router handles basic functional based view routing.

Arguments:
  • url: The url for this route, e.g: /foo
  • func: The view function
Keyword Arguments:
  • endpoint: (Optional) A custom endpoint name, by default flask uses the view function name.

Example

from flask.ext.via.routers.default import Basic

def foo(bar=None):
    return 'foo'

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

Pluggable Router

The flask_via.routers.default.Pluggable router handles views created using Flasks pluggable views.

Arguments:
  • url: The url for this route, e.g: /foo
Keyword Arguments:
  • view_func: View function

Example

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

class FooView(MethodView):

    def get(self, bar=None):
        return 'foo'

routes = [
    Plugganle('/', view_func=FooView.as_view('foo')),
    Plugganle('/<bar>', view_func=FooView.as_view('foobar')),
]

Flask-Restful Routers

Flask-Restful is an awesome framework for building REST API’s in Flask but has it’s own way of adding routes to the Flask application, so tere is a little bit of extra work required when bootstrapping your application:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from flask import Flask
from flask.ext import restful
from flask.ext.via import Via

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

via = Via()
via.init_app(
    app,
    routes_module='yourapp.routes',
    restful_api=api)

if __name__ == '__main__':
    app.run(debug=True)

Note that on line 12 we passed a keyword argument called restful_api with the value being the Flask-Restful api object into via.init_app. This will allow the flask_via.routers.restful.Resource router to add resouce routes to the api.

Resouce Router

Warning

Before using this router be sure you have read the section directly above.

The flask_via.routers.restful.Resource router allows us to register Flask-Restful resources to your application.

Arguments:
  • url: The url for this route, e.g: /foo
  • resource: A Flask-Restful Resource class
Keyword Arguments:
  • endpoint: (Optional) A custom endpoint name

Example

class FooResource(restful.Resource):

    def get(self, bar=None):
        return {'hello': 'world'}

routes = [
    Resource('/', FooResource)
    Resource('/<bar>', FooResource, endpoint='foobar')
]

Flask-Admin Routers

As with the Flask-Restful router you need to pass an extra argument to via.init_app called flask_admin which should hold the Flask-Admin instance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from flask import Flask
from flask.ext.admin import Admin
from flask.ext.via import Via

app = Flask(__name__)

admin = Admin(name='Admin')
admin.init_app(app)

via = Via()
via.init_app(
    app,
    routes_module='flask_via.examples.admin',
    flask_admin=admin)

if __name__ == '__main__':
    app.run(debug=True)

Note that line 14 is where the instantiated Flask-Admin instance gets passed into via.init_app.

Admin Router

Warning

Before using this router be sure you have read the section directly above.

The flask_via.routers.admin.AdminRoute router allows us to register Flask-Admin views to your application. Flask-Admin handles defining urls for its views so a url argument is not requied, all is required is the Flask-Admin view class.

Arguments:
  • view: An instantiated Flask-Admin view

Example

class FooAdminView(BaseView):

    @expose('/')
    def index(self):
        return 'foo'

    @expose('/bar')
    def index(self):
        return 'bar'


routes = [
    AdminRoute(FooAdminView(name='Foo'))
]