Skip to main content

A library to help dynamically register routes in Flask applications.

Project description

Flask dynamic route registration

PyPI - License PyPI - Python Version PyPI - Version

A library to help dynamically register routes in Flask applications.

pip install flask-dynamic-route-registration

How to Use

Basic Setup

You start by creating a Flask application as you usually do.

from flask import Flask
from flask_dynamic_route_registration import register_blueprint

app = Flask(__name__)

register_blueprint(app, "healthcheck", '')

app.run()

Inside the healthcheck/__init__.py file

from flask import make_response
from typing import TYPE_CHECKING

from dynamic_route_registration import register_route

if TYPE_CHECKING:
    from flask import Response

@register_route("/health")
def health() -> "Response":
    response = make_response("OK", 200)
    response.mimetype = "text/plain"
    return response
    

More advanced use cases

In those use cases, we will cover both:

  • how to reuse the same file multiple times but with different parameters,
  • how to register a route based on a condition
api_versions = [
    {
        "blueprint_name": "api_v1",
        "blueprint_kwargs": {"url_prefix": "/api/1"},
        "params": {"version": 1},
    },
    {
        "blueprint_name": "api_v2",
        "blueprint_kwargs": {"url_prefix": "/api/2"},
        "params": {"version": 2},
    }
]

register_blueprint(app, 'api', api_versions)

Inside the api/__init__.py file

from flask import jsonify
from typing import TYPE_CHECKING

from dynamic_route_registration import register_route

if TYPE_CHECKING:
    from flask import Response

@register_route("/status")
def status(*, version: int = 1) -> "Response":
    return jsonify({"status": "OK", "version": version})
    

@register_route("/foo", enabled=lambda **params: params.get("version", 1) >= 2)
def foo_a(*, version: int = 1) -> "Response":
    return jsonify({"hello": "world"})

@register_route("/foo/<subject>", enabled=lambda **params: params.get("version", 1) >= 2)
def foo_b(subject: str = "world", version: int = 1) -> "Response":
    return jsonify({"hello": subject})

By doing so we have registered four routes:

URL Return value Function
/api/1/status {"status": "OK", "version": 1} status
/api/2/status {"status": "OK", "version": 2} status
/api/2/foo {"hello": "world"} foo_a
/api/2/foo/<subject> {"hello": <subject>} foo_b

Other examples

The register_route decorator also accept commons flask.route parameters like methods

@register_route("/post", methods=["POST"])
def post_endpoint () -> "Response":
    return jsonify({"hello": "world"})

Limitations

As for now you can't register multiple routes at the same time for a given function.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

flask_dynamic_route_registration-0.9.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file flask_dynamic_route_registration-0.9.1.tar.gz.

File metadata

File hashes

Hashes for flask_dynamic_route_registration-0.9.1.tar.gz
Algorithm Hash digest
SHA256 02fc152bb599057fd3fbd85ce89e2efcb041406ea68a3f021f7b3c5331e7669a
MD5 e38a4bd5a73ca7042a8f137918dc4b0e
BLAKE2b-256 4a6dfecd1f3c0093d13508fae1f920bde84148b9c7a95ff7d15742bef7c4ba0d

See more details on using hashes here.

File details

Details for the file flask_dynamic_route_registration-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_dynamic_route_registration-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25e65adeb9059f14655df03844bf47fac14c712de970e59727fda73177b4dd1b
MD5 246390e8aedb05e3a8817d0d2332cc1f
BLAKE2b-256 9a38dbd8f01c4dd039342d61ef8bcf745915a848b9acb2a65aa2ecca0d7da687

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page