Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-framework: MVC pattern

Does Flask framework support MVC pattern naturally? What the part of application should I consider as a model, what as a view and what as a controller?

Typically (in my experience) a Flask app looks like this:

main_dir--|
          |
         app1--|
          |    |
          |  __init__.py
          |  api.py
          |  models.py
          |
         static--|
          |      |
          |    all the static stuff
          |
         app.py # with blueprints registering
like image 983
I159 Avatar asked Sep 22 '12 19:09

I159


People also ask

Does Flask support MVC framework?

Flask is actually not an MVC framework.

Is Flask an MVC architecture?

Project StructureFlask follows MVC architecture whereas Django follows MVT architecture and both have their core differences. Flask Project is a single application where you can add countless views and models. Django project is a collection of smaller applications.

What pattern does Flask use?

Flusk. Flusk is an example that you can use to create big Flask applications that include SQLAlchemy, Docker, and Nginx. It has a beautiful logic separation to create backend, domain, views, and models into their respective layers. It has excellent use of Flask Blueprints and follows the Factory design pattern.

What is MVC structure Flask?

Model–View–Controller (MVC) is an architectural pattern for implementing user interfaces. It divides an application into three interconnected parts: the Model, the View, and the Controller.


2 Answers

Flask is actually not an MVC framework. It is a minimalistic framework which gives you a lot of freedom in how you structure your application, but MVC pattern is a very good fit for what Flask provides, at least in the way that MVC pattern is understood today in the context of web applications (which purists would probably object to).

Essentially you write your methods and map them to specific route, e.g.:

@app.route("/")
def hello():
    return "Hello World!"

No view or model there, as you can see. However, it is also built on top of Jinja2 template library, so in a realistic app, your method (which acts as a controller) looks like:

@app.route("/")
def hello():
    return render_template('index.html', username="John Doe")

Here, you use index.html template to render the page. That is your view now.

Flask doesn't prescribe any model. You can use whatever you want - from complex object models (typically with using some ORM like SQLAlchemy) to simplest thing which fits your needs.

And there you have it: MVC

like image 138
Zdeslav Vojkovic Avatar answered Oct 23 '22 18:10

Zdeslav Vojkovic


To look at Flask from a MVC perspective, I think Flask gives us flexibility to implement our own Model or Views. But for the Controller we need to rely on the Flask framework itself.

1.    @app.route("/")
2.    def hello():
2.1       # Code for your model here
2.2       # model code
3.        return render_template('index.html', username="John Doe")

In the above code -

  1. line 1 - (invoked by) Controller: is what the Flask Controller calls,
  2. line 2 - Model: this is where we code our own implementation for defining the Model,
  3. line 3 - View: we can code our View as index.html coded with {{ }} and {% %} with Model data being passed to View as form of Dict or user object. e.g. username="Xxx"
like image 4
d5t Avatar answered Oct 23 '22 16:10

d5t