
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Transform your Flask application into a structured MVC architecture with powerful CLI tools
Installation • Quick Start • Documentation • Examples • Contributing
pip install flask_mvc
poetry add flask_mvc
git clone https://github.com/marcuxyz/flask-mvc.git
cd flask_mvc
poetry install
from flask import Flask
from flask_mvc import FlaskMVC
app = Flask(__name__)
FlaskMVC(app)
if __name__ == "__main__":
app.run(debug=True)
from flask import Flask
from flask_mvc import FlaskMVC
mvc = FlaskMVC()
def create_app():
app = Flask(__name__)
# Initialize MVC extension
mvc.init_app(app, path='src') # Custom path (default: 'app')
return app
app = create_app()
# Generate a basic controller
flask mvc generate controller home
# Generate controller in custom path
flask mvc generate controller user --path src/controllers
# Force overwrite existing controller
flask mvc generate controller admin --force
This creates a professional controller with CRUD operations:
"""HomeController - Generated by Flask MVC CLI."""
from flask import render_template, jsonify, request
from typing import Any, Dict, Optional, Union
class HomeController:
"""Controller for handling home related requests."""
def index(self) -> Union[str, Dict[str, Any]]:
"""Display the index page."""
if request.is_json or request.accept_mimetypes.accept_json:
return jsonify({
"message": "Hello from HomeController!",
"controller": "home",
"action": "index"
})
return "Hello from HomeController!"
# Complete CRUD methods included...
Flask MVC encourages a clean project structure:
your-project/
├── app/ # Main application directory
│ ├── __init__.py
│ ├── controllers/ # Controllers directory
│ │ ├── __init__.py
│ │ ├── home_controller.py
│ │ └── user_controller.py
│ ├── models/ # Models directory (optional)
│ │ └── user.py
│ ├── views/ # Templates directory
│ │ ├── layouts/
│ │ ├── home/
│ │ └── user/
│ └── routes.py # Route definitions
├── tests/ # Test directory
├── requirements.txt # Dependencies
└── app.py # Application entry point
Flask MVC provides powerful CLI commands for rapid development:
# Basic controller
flask mvc generate controller blog
# API controller
flask mvc generate controller api_v1_users
# Controller with custom path
flask mvc generate controller admin --path admin/controllers
# Force overwrite
flask mvc generate controller posts --force
Option | Short | Description |
---|---|---|
--path | -p | Custom path for generated files |
--force | -f | Overwrite existing files |
--help | -h | Show command help |
class BlogController:
def index(self):
posts = Post.get_all()
return render_template('blog/index.html', posts=posts)
def show(self, id: int):
post = Post.get_by_id(id)
return render_template('blog/show.html', post=post)
class ApiUserController:
def index(self):
users = User.get_all()
return jsonify([user.to_dict() for user in users])
def create(self):
data = request.get_json()
user = User.create(data)
return jsonify(user.to_dict()), 201
Generated controllers automatically handle both web and API requests:
def index(self) -> Union[str, Dict[str, Any]]:
posts = Post.get_all()
if request.is_json or request.accept_mimetypes.accept_json:
return jsonify([post.to_dict() for post in posts])
return render_template('posts/index.html', posts=posts)
Customize Flask MVC behavior using environment variables:
# Custom paths
export FLASK_MVC_CONTROLLERS_PATH="src/controllers"
export FLASK_MVC_VIEWS_PATH="src/templates"
export FLASK_MVC_MODELS_PATH="src/models"
# Template settings
export FLASK_MVC_TEMPLATES_DIR="custom/templates"
export FLASK_MVC_FILE_ENCODING="utf-8"
from flask_mvc.core.config import CLIConfig
# Override default settings
CLIConfig.DEFAULT_CONTROLLERS_PATH = "src/controllers"
CLIConfig.DEFAULT_VIEWS_PATH = "src/templates"
Flask MVC is built with testing in mind:
import pytest
from flask_mvc.core.generators import ControllerGenerator
from flask_mvc.core.exceptions import InvalidControllerNameError
def test_controller_generation():
generator = ControllerGenerator()
# Test valid controller generation
result = generator.generate("test", "/tmp/controllers")
assert result.exists()
# Test invalid name handling
with pytest.raises(InvalidControllerNameError):
generator.generate("123invalid")
We welcome contributions! Please see our Contributing Guidelines for details.
# Clone the repository
git clone https://github.com/marcuxyz/flask-mvc.git
cd flask_mvc
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Run linting
poetry run black .
poetry run flake8
# Build documentation
poetry run mkdocs serve
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
Transform Flask into a structured MVC architecture with powerful CLI tools
We found that flask-mvc2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.