Skip to main content

turn standard Flask into mvc

Project description

GitHub code size in bytes GitHub Workflow Status GitHub PyPI - Downloads PyPI - Python Version PyPI

You can use the mvc pattern in your flask application using this extension.

Installation

Run the follow command to install mvc_flask:

$ pip install mvc_flask

Configuration

To configure the mvc_flask you need import and register in your application, e.g:

from flask import Flask
from mvc_flask import FlaskMVC

app = Flask(__name__)
FlaskMVC(app)

Or use application factories, e.g:

mvc = FlaskMVC()

def create_app():
  ...
  mvc.init_app(app)

By default the mvc_flask assumes that your application directory will be app and if it doesn't exist, create it!

You structure should be look like this:

app
├── __ini__.py
├── controllers
│   └── home_controller.py
├── routes.py
└── views
    ├── index.html

Router

You can create routes in app/routes.py and after create file, you can start register routes, e.g:

from mvc_flask import Router

Router.get("/", "home#index")

The same must be make done to POST, PUT and DELETE methods. E.g: Router.post("/messages", "messages#create")

The first param represent the relative path and second represent the controller#action. Remember that we are working with MVC pattern, so we have controller and action.

The controller can be created in app/controllers and action is method of controller.

You can use Router.all() to register all routes of CRUD.

Router.all("users")

The previous command produce this:

users.create     POST     /users
users.delete     DELETE   /users/<id>
users.edit       GET      /users/<id>/edit
users.index      GET      /users
users.new        GET      /users/new
users.show       GET      /users/<id>
users.update     PUT      /users/<id>

You can also use only parameter to controll routes, e.g:

Router.all("messages", only="index show new create")

The previous command produce this:

messages.create  POST     /messages
messages.index   GET      /messages
messages.new     GET      /messages/new
messages.show    GET      /messages/<id>

The paramenter only accept string or array, so, you can use only=["index", "show", "new", "create"]

Controller

Now that configure routes, the home_controller.py file must contain the HomeController class, registering the action, e.g:

class HomeController:
    def index(self, view, request):
        return view("index.html")

If you have question, please, check de app directory to more details.

To use the hooks as before_request, after_request and etc... Just describe it in the controller, see:

class HomeController:
    before_request = ["hi"]

    def index(self, view, request):
        return "home"

    def hi(self):
        ...

The previous example describes the hi(self) will be called every times that the visitors access the controller.

Views

Flask use the templates directory by default to store HTMLs files. However, using the mvc-flask the default becomes views. You can use the app/views directory to stores templates.

Tests

You can run the tests, executing the follow command:

$ make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mvc-flask-2.3.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

mvc_flask-2.3.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file mvc-flask-2.3.1.tar.gz.

File metadata

  • Download URL: mvc-flask-2.3.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Darwin/21.1.0

File hashes

Hashes for mvc-flask-2.3.1.tar.gz
Algorithm Hash digest
SHA256 7ce0f7b7c70d4a19d9de7758c62400fc8f443a769f4cbe9fcee9e4fb7cdf4aaa
MD5 f59ff0fb8677e94afdfdf163a34e7a03
BLAKE2b-256 7ea24293b2bf6bd3455442a3a77529bc5a868086b22a21492e7d9501a696fdc2

See more details on using hashes here.

File details

Details for the file mvc_flask-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: mvc_flask-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Darwin/21.1.0

File hashes

Hashes for mvc_flask-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0707e57e8697ef3f8ee163e734f6ace0927c792228f5fe1fbed278fca70b2e10
MD5 540e08b8f3d8b4dca717a8dd6c9bc917
BLAKE2b-256 658864816b7c5fc39ffd8bc6f9819e31af86ef0da2b798b6d8c9b1b92b529755

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page