Flask Dynamic Router
A Flask extension that provides automatic and dynamic route registration for Flask applications.
Installation
pip install flask-dynamic-router
How to Use
Basic Setup
from flask import Flask
from flask_dynamic_router import DynamicRouter
# Create Flask app
app = Flask(__name__)
# Initialize the router
router = DynamicRouter(app)
# Register routes from a directory
router.register_routes('routes')
if __name__ == '__main__':
app.run()
Route Examples
- Basic Route (
routes/Root/__init__.py):
from flask import Blueprint
main = Blueprint('root', __name__)
@main.route('/')
def index():
return {'message': 'Welcome to the API'}
- Dynamic Parameter (
routes/Users/[userID]/__init__.py):
from flask import Blueprint
main = Blueprint('user_detail', __name__)
@main.route('/')
def get_user(user_id):
return {'message': f'User details for ID: {user_id}'}
@main.route('/profile')
def get_profile(user_id):
return {'message': f'Profile for user ID: {user_id}'}
- Nested Routes (
routes/Products/[productID]/reviews/__init__.py):
from flask import Blueprint
main = Blueprint('product_reviews', __name__)
@main.route('/')
def get_reviews(product_id):
return {'message': f'Reviews for product ID: {product_id}'}
Configuration Options
# Make routes case-insensitive
app.config['DYNAMIC_ROUTER_CASE_SENSITIVE'] = False
# Add a global prefix to all routes
app.config['DYNAMIC_ROUTER_URL_PREFIX'] = '/api/v1'
Directory Structure
The router follows a convention-based approach where your directory structure maps directly to URL routes:
📦 routes
├── 📂 Root
│ ├── 📄 __init__.py ➜ 🌐 /
│ ├── 📂 version
│ │ └── 📄 __init__.py ➜ 🌐 /version
│ └── 📂 about
│ └── 📄 __init__.py ➜ 🌐 /about
│
├── 📂 Users
│ ├── 📄 __init__.py ➜ 🌐 /users
│ ├── 📂 [userID] ➜ 💫 Dynamic Parameter
│ │ ├── 📄 __init__.py ➜ 🌐 /users/<user_id>
│ │ └── 📂 profile
│ │ └── 📄 __init__.py ➜ 🌐 /users/<user_id>/profile
│ └── 📂 settings
│ └── 📄 __init__.py ➜ 🌐 /users/settings
│
└── 📂 Products
├── 📄 __init__.py ➜ 🌐 /products
└── 📂 [productID] ➜ 💫 Dynamic Parameter
└── 📄 __init__.py ➜ 🌐 /products/<product_id>
Route Mapping Examples
| Directory Structure | Generated Route | Type |
|---|---|---|
Root/__init__.py |
/ |
Static Route |
Users/__init__.py |
/users |
Static Route |
Users/[userID]/__init__.py |
/users/<user_id> |
Dynamic Route |
Products/[productID]/__init__.py |
/products/<product_id> |
Dynamic Route |
Dynamic Parameters
Dynamic parameters are defined using square brackets in directory names:
- 📂
[userID]➜<user_id> - 📂
[productID]➜<product_id> - 📂
[categoryName]➜<category_name>
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for flask-dynamic-router 0.1.0
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_router-0.1.0.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_dynamic_router-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / flask_dynamic_router-0.1.0.tar.gz
| Download URL | flask_dynamic_router-0.1.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
853c0f94ef9bef7124be751bd0856160400621c93b8382f642d434d004687599
|
|
BLAKE2b-256 checksum How to use checksums |
1ffae233e749928008ffff443d35d0593ee0a8c2d894ad171a2e4d659565cedb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.1
|
Release files / flask_dynamic_router-0.1.0-py3-none-any.whl
| Download URL | flask_dynamic_router-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60bc7f1983584ea4b1bf591baf6c885e7b46a491e9e6f63abb70868e72575028
|
|
BLAKE2b-256 checksum How to use checksums |
05296a2d79c66649ad4688e4d767a25d0f3a510681e812bd4718772558e3920b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.1
|