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.Functional router handles basic functional based view routing.
from flask.ext.via.routers.default import Functional
def foo(bar=None):
return 'foo'
routes = [
Functional('/', foo),
Functional('/<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('/', FooView, 'foo'),
Plugganle('/<bar>', FooView, '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 your application.
class FooResource(restful.Resource):
def get(self, bar=None):
return {'hello': 'world'}
routes = [
Resource('/', FooResource)
Resource('/<bar>', FooResource, endpoint='foobar')
]
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.
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.
class FooAdminView(BaseView):
@expose('/')
def index(self):
return 'foo'
@expose('/bar')
def index(self):
return 'bar'
routes = [
AdminRoute(FooAdminView(name='Foo'))
]