A lightweight Flask extension that brings Next.js-style API routing to Flask applications. Organize your Flask APIs using file-based routing, just like Next.js!
- π File-based routing similar to Next.js
- π Automatic HTTP method detection from filenames
- π Simple integration with existing Flask apps
- 0οΈβ£ Zero additional dependencies beyond Flask
- π― Convention over configuration
- π§ Easy to extend and customize
# Currently local installation
git clone https://github.com/napjon/flext
cd flext
pip install -e .- Create your API structure:
your_app/
βββ app.py
βββ api/
β βββ hello/
β β βββ get.py
β β βββ post.py
- Write your endpoint in
api/hello/get.py:
def main(request):
return {"message": "Hello World!"}- Setup your Flask app (
app.py):
from flask import Flask
from flext import Flext
app = Flask(__name__)
app = Flext(app)
if __name__ == '__main__':
app.run(debug=True)That's it! Your API endpoints are now available at:
GET /api/helloPOST /api/hello
Each endpoint should have a main function that accepts a Flask request object:
def main(request):
"""
Args:
request (flask.Request): HTTP request object.
Returns:
Response: Any Flask-compatible response
"""
return {"status": "success"}Files are automatically mapped to HTTP methods:
get.pyβ GETpost.pyβ POSTput.pyβ PUTdelete.pyβ DELETEpatch.pyβ PATCH
Customize Flext behavior during initialization:
app = Flext(
app,
api_dir='custom_api_dir', # Default: 'api'
prefix='/api/v1', # Default: '/api'
debug=True # Default: False
)pip install gunicorn
gunicorn app:appFROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["gunicorn", "app:app"]Modify the entry point for Google Cloud Functions:
# main.py
from flask import Flask
from flext import Flext
app = Flask(__name__)
app = Flext(app)
# For Cloud Functions
def entry_point(request):
return app(request)Create a global error handler:
# api/_middleware.py
def handle_errors(app):
@app.errorhandler(404)
def not_found(e):
return {"error": "Not found"}, 404
@app.errorhandler(500)
def server_error(e):
return {"error": "Internal server error"}, 500Validate incoming requests:
# api/users/post.py
from flask import jsonify
def main(request):
if not request.is_json:
return jsonify({"error": "Content-Type must be application/json"}), 400
data = request.get_json()
required = ['napjon', 'email']
if not all(k in data for k in required):
return jsonify({"error": "Missing required fields"}), 400
return jsonify({"status": "user created"})Use Flask's application context:
# api/users/get.py
from flask import current_app
def main(request):
db = current_app.config['DB']
users = db.query(...)
return jsonify(users)flext/
βββ flext.py # Main library code
βββ setup.py # Package setup
βββ requirements.txt # Dependencies
βββ tests/ # Test cases
pip install pytest
pytest tests/-
Middleware Support
- Add support for middleware files (e.g.,
_middleware.py) - Enable global and route-specific middleware
- Add support for middleware files (e.g.,
-
API Documentation
- Automatic OpenAPI/Swagger documentation generation
- Integration with Flask-RESTX
-
Hot Reloading
- Implement hot reloading for API routes
- Development server with auto-reload
-
Type Hints
- Add comprehensive type hints
- Runtime type checking
-
Route Validation
- Schema validation support
- Pydantic integration
-
CLI Tool
- Scaffold new API endpoints
- Generate boilerplate code
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Clone repository
git clone https://github.com/napjon/flext
cd flext
# Create virtual environment
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txt
# Run tests
pytest- π Documentation
- π Issue Tracker
- π» Examples
MIT License - see LICENSE for details
- Inspired by Next.js API routes
- Built on top of Flask
- Made with β€οΈ by the community