Skip to content

Introduction

This extension facilitates the application of this design pattern in Flask

unit test PyPI version

Designed to allow developers to implement the Model-View-Controller (MVC) design pattern in Flask applications with the help of this extension.


Install Flask MVC using pip:

$ pip install flaskmvc

Install Flask MVC using UV:

$ uv add flaskmvc

Now, let's get started:

from flask import Flask
from flask_mvc import FlaskMVC
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'

    # registering extensions
    FlaskMVC(app)
    db.init_app(app)

    return app

Image of structure

Features

Flask MVC builds on provides the best architecture experience for Flask, and gives you:

  • You can directories as controllers, models, and views.
  • It Supports the controllers' creation, and you can separate the logic of your application of business rules
  • You can separate routes of business rules
  • You can use the before_action to execute a specific code
  • You can integrate with other extensions of Flask, Flask-SQLAlchemy, Flask-Migrate, etc.
  • SQLAlchemy Introspection: deferred model reflection support with ReflectedModel and FlaskReflection for runtime schema discovery.

Dependencies

Flask MVC just depends on the Flask extensions to working and requires Python >=3.8.0,<4.0.0.

SQLAlchemy Introspection

This extension now supports deferred SQLAlchemy reflection for models. By inheriting from ReflectedModel and using the FlaskReflection integration, models can map to existing database tables without declaring every column in the model class.

Example:

from flask_mvc.middlewares.base_model import ReflectedModel
from tests.app import db

class Message(ReflectedModel, db.Model):
    __tablename__ = 'messages'

Initialize the database extension before Flask MVC so introspection works correctly:

from flask import Flask
from flask_mvc import FlaskMVC
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'

db.init_app(app)
FlaskMVC(app, path='app', db=db)