flask-dynamic-route-registration


Nameflask-dynamic-route-registration JSON
Version 0.9.0 PyPI version JSON
download
home_pageNone
SummaryA library to help dynamically register routes in Flask applications.
upload_time2025-02-06 17:13:58
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords flask route dynamic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask dynamic route registration

A library to help dynamically register routes in Flask applications.

```bash
pip install flask-dynamic-route-registration
```

## How to Use
### Basic Setup

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

```python
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


```python
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

```python
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


```python
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

```python

@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](https://github.com/jeromediaz/flask-dynamic-route-registration/blob/main/LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "flask-dynamic-route-registration",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "flask, route, dynamic",
    "author": null,
    "author_email": "J\u00e9r\u00f4me DIAZ <jerome_diaz@me.com>",
    "download_url": "https://files.pythonhosted.org/packages/65/2a/48b82b260ac3384b026f74c62846db6f4c673fcd6d88e323bf92ce5d458f/flask_dynamic_route_registration-0.9.0.tar.gz",
    "platform": null,
    "description": "# Flask dynamic route registration\n\nA library to help dynamically register routes in Flask applications.\n\n```bash\npip install flask-dynamic-route-registration\n```\n\n## How to Use\n### Basic Setup\n\nYou start by creating a Flask application as you usually do.\n\n```python\nfrom flask import Flask\nfrom flask_dynamic_route_registration import register_blueprint\n\napp = Flask(__name__)\n\nregister_blueprint(app, \"healthcheck\", '')\n\napp.run()\n```\n\nInside the `healthcheck/__init__.py` file\n\n\n```python\nfrom flask import make_response\nfrom typing import TYPE_CHECKING\n\nfrom dynamic_route_registration import register_route\n\nif TYPE_CHECKING:\n    from flask import Response\n\n@register_route(\"/health\")\ndef health() -> \"Response\":\n    response = make_response(\"OK\", 200)\n    response.mimetype = \"text/plain\"\n    return response\n    \n```\n\n\n### More advanced use cases\nIn those use cases, we will cover both:\n- how to reuse the same file multiple times but with different parameters,\n- how to register a route based on a condition\n\n```python\napi_versions = [\n    {\n        \"blueprint_name\": \"api_v1\",\n        \"blueprint_kwargs\": {\"url_prefix\": \"/api/1\"},\n        \"params\": {\"version\": 1},\n    },\n    {\n        \"blueprint_name\": \"api_v2\",\n        \"blueprint_kwargs\": {\"url_prefix\": \"/api/2\"},\n        \"params\": {\"version\": 2},\n    }\n]\n\nregister_blueprint(app, 'api', api_versions)\n```\n\n\n\nInside the `api/__init__.py` file\n\n\n```python\nfrom flask import jsonify\nfrom typing import TYPE_CHECKING\n\nfrom dynamic_route_registration import register_route\n\nif TYPE_CHECKING:\n    from flask import Response\n\n@register_route(\"/status\")\ndef status(*, version: int = 1) -> \"Response\":\n    return jsonify({\"status\": \"OK\", \"version\": version})\n    \n\n@register_route(\"/foo\", enabled=lambda **params: params.get(\"version\", 1) >= 2)\ndef foo_a(*, version: int = 1) -> \"Response\":\n    return jsonify({\"hello\": \"world\"})\n\n@register_route(\"/foo/<subject>\", enabled=lambda **params: params.get(\"version\", 1) >= 2)\ndef foo_b(subject: str = \"world\", version: int = 1) -> \"Response\":\n    return jsonify({\"hello\": subject})\n```\n\nBy doing so we have registered four routes:\n\n| URL                    | Return value                   | Function |\n|------------------------|--------------------------------|----------|\n| /api/1/status          | {\"status\": \"OK\", \"version\": 1} | status   |\n| /api/2/status          | {\"status\": \"OK\", \"version\": 2} | status   | \n| /api/2/foo             | {\"hello\": \"world\"}             | foo_a    |\n| /api/2/foo/`<subject>` | {\"hello\": `<subject>`}         | foo_b    |\n\n\n### Other examples\n\nThe register_route decorator also accept commons flask.route parameters like methods\n\n```python\n\n@register_route(\"/post\", methods=[\"POST\"])\ndef post_endpoint () -> \"Response\":\n    return jsonify({\"hello\": \"world\"})\n\n```\n\n\n### Limitations\nAs for now you can't register multiple routes at the same time for a given function.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](https://github.com/jeromediaz/flask-dynamic-route-registration/blob/main/LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A library to help dynamically register routes in Flask applications.",
    "version": "0.9.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/jeromediaz/flask-dynamic-route-registration/issues",
        "Homepage": "https://github.com/jeromediaz/flask-dynamic-route-registration"
    },
    "split_keywords": [
        "flask",
        " route",
        " dynamic"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "fda050d333c61359a95001124cd0c12ede7897a868f0f4d07305bb803139de0e",
                "md5": "5696d9390d7b4fd51863b6471af320e8",
                "sha256": "4b080332f1db3ea419678665c19cbaa869bb421054a8f0430fdbeeeda2fa9647"
            },
            "downloads": -1,
            "filename": "flask_dynamic_route_registration-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5696d9390d7b4fd51863b6471af320e8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6030,
            "upload_time": "2025-02-06T17:13:56",
            "upload_time_iso_8601": "2025-02-06T17:13:56.927913Z",
            "url": "https://files.pythonhosted.org/packages/fd/a0/50d333c61359a95001124cd0c12ede7897a868f0f4d07305bb803139de0e/flask_dynamic_route_registration-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "652a48b82b260ac3384b026f74c62846db6f4c673fcd6d88e323bf92ce5d458f",
                "md5": "9f6ac5ba6f9baa621e2ad1b1d0727e92",
                "sha256": "a8058cea9858ed489f84216edcd5331fc5dc42e7ea42d1bc07e06a219ebc11d2"
            },
            "downloads": -1,
            "filename": "flask_dynamic_route_registration-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9f6ac5ba6f9baa621e2ad1b1d0727e92",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4958,
            "upload_time": "2025-02-06T17:13:58",
            "upload_time_iso_8601": "2025-02-06T17:13:58.248706Z",
            "url": "https://files.pythonhosted.org/packages/65/2a/48b82b260ac3384b026f74c62846db6f4c673fcd6d88e323bf92ce5d458f/flask_dynamic_route_registration-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-06 17:13:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeromediaz",
    "github_project": "flask-dynamic-route-registration",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "flask-dynamic-route-registration"
}
        
Elapsed time: 0.69441s