A library to help dynamically register routes in Flask applications.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_dynamic_route_registration-0.9.1.tar.gz.
File metadata
- Download URL: flask_dynamic_route_registration-0.9.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02fc152bb599057fd3fbd85ce89e2efcb041406ea68a3f021f7b3c5331e7669a
|
|
| MD5 |
e38a4bd5a73ca7042a8f137918dc4b0e
|
|
| BLAKE2b-256 |
4a6dfecd1f3c0093d13508fae1f920bde84148b9c7a95ff7d15742bef7c4ba0d
|
File details
Details for the file flask_dynamic_route_registration-0.9.1-py3-none-any.whl.
File metadata
- Download URL: flask_dynamic_route_registration-0.9.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25e65adeb9059f14655df03844bf47fac14c712de970e59727fda73177b4dd1b
|
|
| MD5 |
246390e8aedb05e3a8817d0d2332cc1f
|
|
| BLAKE2b-256 |
9a38dbd8f01c4dd039342d61ef8bcf745915a848b9acb2a65aa2ecca0d7da687
|