Here you will find the documentation for each bundled router provided by Flask-Via.
These routers are designed to work with standard flask functional and class based pluggable views.
The flask_via.routers.default.Basic router handles basic functional based view routing.
from flask.ext.via.routers.default import Basic
def foo(bar=None):
return 'foo'
routes = [
Basic('/', foo),
Basic('/<bar>', foo, endpoint='foobar'),
]
The flask_via.routers.default.Pluggable router handles views created using Flasks pluggable views.
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 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.
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 our application.
class FooResource(restful.Resource):
def get(self, bar=None):
return {'hello': 'world'}
routes = [
Resource('/', FooResource)
Resource('/<bar>', FooResource, endpoint='foobar')
]