A Flask extension for dynamic and automatic route registration
Project description
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.
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_router-0.1.0.tar.gz.
File metadata
- Download URL: flask_dynamic_router-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
853c0f94ef9bef7124be751bd0856160400621c93b8382f642d434d004687599
|
|
| MD5 |
7f1a228f56fd1b8b26fdf51852e14f09
|
|
| BLAKE2b-256 |
1ffae233e749928008ffff443d35d0593ee0a8c2d894ad171a2e4d659565cedb
|
File details
Details for the file flask_dynamic_router-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flask_dynamic_router-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60bc7f1983584ea4b1bf591baf6c885e7b46a491e9e6f63abb70868e72575028
|
|
| MD5 |
e5af83abab95c9d3684e7560544f3756
|
|
| BLAKE2b-256 |
05296a2d79c66649ad4688e4d767a25d0f3a510681e812bd4718772558e3920b
|