Skip to main content

Convenient routing for a flask application

Project description

Flouter (Flask Router) is a convenience add-on for the Flask library. It converts a directory structure into valid routes for a Flask application. This allows developers to quickly layout complex applications, and easily navigate to existing code. This library is under heavy development and may not yet support a feature you need. If that is the case, please submit a feature request so the library can continue to improve.

Installing

Install and update using pip:

pip install -U flouter

Basic Usage

Flouter will convert the following routes directory structure…

routes/
|-- api/
    |-- index.py
    |-- echo.py
    |-- _foo.py

…to the corresponding routes in a flask application.

/api/
/api/echo/
/api/<foo>

In one of these files, methods are defined by simple named functions that are called when the appropriate HTTP request is passed to the route.

# echo.py

def get():
    return 'Hello World'

def post(request):
    # returns are turned into valid responses by the library
    return request.json

An example usage of this library is included in examples/basic

# main.py
import os

from flask import Flask
from src.flouter import Router

app = Flask(__name__)

route_dir = os.getcwd() + "/routes/"
router = Router(route_dir)

router.register_routes(app)

app.run()

A Router object also allows route_params to be defined, which allow methods to access important elements of an application without having to explicitly import these into every single file, which could quickly become annoying. The default value for this dictionary contains only the flask.request object, which you can access in any function by adding the request kwarg. However, you can extend this to pass in any important variables you may have.

# main.py

d = dict(
    my_constant=10,
)

router = Router(directory, route_params=d)

Which enables my_constant to be passed to any route child function.

# /api/index.py

def get(my_constant):
    return my_constant

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flouter-0.0.6.dev0.tar.gz (6.7 kB view hashes)

Uploaded source

Built Distribution

flouter-0.0.6.dev0-py2.py3-none-any.whl (7.5 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page