turn standard Flask into mvc
Project description
You can use the mvc pattern in your flask application using this extension.
This real world implementation FLASK MVC example: https://github.com/negrosdev/negros.dev
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!
If you can use other directory, you can use the path paramenter when the instance of FlaskMVC is initialized. E.g:
mvc = FlaskMVC()
def create_app():
...
mvc.init_app(app, path='src')
Now, you can use src as default directory for prepare your application.
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("messages")
The previous command produce this:
messages.create POST /messages
messages.delete DELETE /messages/<id>
messages.edit GET /messages/<id>/edit
messages.index GET /messages
messages.new GET /messages/new
messages.show GET /messages/<id>
messages.update PATCH, PUT /messages/<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.index GET /messages
messages.show GET /messages/<id>
messages.new GET /messages/new
messages.create POST /messages
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):
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):
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.
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 mvc-flask-2.4.0.tar.gz.
File metadata
- Download URL: mvc-flask-2.4.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae90bb62516cfa38b11ad295c49cf304cf9d006857eb62d9296d561bf745c4cd
|
|
| MD5 |
f96abd4045a28d97175e11f6a53f5ff1
|
|
| BLAKE2b-256 |
2577a262354226da7d25c4438efbd8a74c9395fa17ce949fc70c222e5931634a
|
File details
Details for the file mvc_flask-2.4.0-py3-none-any.whl.
File metadata
- Download URL: mvc_flask-2.4.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af6bbc27646ce2224d82ba146545dee4ec5253477d2b6bc1220674b2a81d4fad
|
|
| MD5 |
d23ad48309daec9f1c85ed3270441d45
|
|
| BLAKE2b-256 |
621083af2c2482c70a5ab7125172216b72506da87c5cd8a8fca4682baafb4419
|