Flask dynamic route registration
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.
Metadata
Release files for flask-dynamic-route-registration 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask_dynamic_route_registration-0.9.1.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_dynamic_route_registration-0.9.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / flask_dynamic_route_registration-0.9.1.tar.gz
| Download URL | flask_dynamic_route_registration-0.9.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02fc152bb599057fd3fbd85ce89e2efcb041406ea68a3f021f7b3c5331e7669a
|
|
BLAKE2b-256 checksum How to use checksums |
4a6dfecd1f3c0093d13508fae1f920bde84148b9c7a95ff7d15742bef7c4ba0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / flask_dynamic_route_registration-0.9.1-py3-none-any.whl
| Download URL | flask_dynamic_route_registration-0.9.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
25e65adeb9059f14655df03844bf47fac14c712de970e59727fda73177b4dd1b
|
|
BLAKE2b-256 checksum How to use checksums |
9a38dbd8f01c4dd039342d61ef8bcf745915a848b9acb2a65aa2ecca0d7da687
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|