Flask-framework: MVC pattern

33,535

Solution 1

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

Solution 2

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"

Solution 3

Also, take a look at this. It is a small flask-based MVC structure project, and works just fine.

Share:
33,535

Related videos on Youtube

I159
Author by

I159

Updated on February 25, 2022

Comments

  • I159
    I159 about 2 years

    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
    
    • wenzul
      wenzul over 8 years
      May also look into this suggestion. github.com/salimane/flask-mvc
    • Ciasto piekarz
      Ciasto piekarz almost 7 years
      Hey @wenzul model is data provible for updating the view directly, but int he example link above the view is getting updated by call to flask.flash which in tunrs update layout.html template.
  • Tom Stambaugh
    Tom Stambaugh about 6 years
    Um ... I'm sorry, but whatever this is, it is not MVC. Perhaps you intend "minimalist framework" to mean "No classes or instances at all". An MVC framework generally includes a hierarchy of "Model" and "View" classes, and provides a mechanism for using events (usually associated with user gestures) to associate an instance of a Model descendant with a View descendant. The example you provide here (the accepted answer I see) has none of this. It is not, therefore an "MVC framework" in any meaningful (to me) way. This example seems to have discarded the baby with the bathwater.
  • Zdeslav Vojkovic
    Zdeslav Vojkovic about 6 years
    "Flask is actually not an MVC framework" I thought this was clear. Now, regarding my example: I didn't call it an MVC framework - I wrote: "there you have it: MVC". And it is true because MVC pattern originally doesn't involve any M/V class hierarchy, neither it requires any events or actually classes. You are conflating a very specific implementation of MVC pattern with the general idea of the pattern. MVC framework != MVC pattern. I took care to use appropriate names.
  • Tom Stambaugh
    Tom Stambaugh about 6 years
    It sounds like we're in violent agreement. The phrase "MVC pattern" is well-understood and documented, see Gang Of Four ("Design Patterns: Elements of Reusable Object Oriented Software", 1995) page 4. That design pattern has been repeated in dozens of frameworks since then. Perhaps it's a simple miscommunication about what we mean by "MVC pattern". "MVC" means much more than calling one function a "model" and another a "controller". There is a constructive discussion to be had regarding how models and views are affected by user gestures. Something more than the above is needed.
  • Zdeslav Vojkovic
    Zdeslav Vojkovic about 6 years
    In MVC web frameworks there is no user gesture in usual sense, so user gestures are not a necessary part. MVC is not one of GoF patterns, it is only vaguely discussed there. Flask actually fits that description very well as Jinja templates are indeed a "screen representation" of the model, while Flask methods "define the way the UI reacts to user inputs". Implementation in modern (web) frameworks vary quite a lot from Smalltalk implementation. Sure, you can wrap both Flask actions and templates in classes. This doesn't make it more or less MVC, as it is irrelevant to the concept/pattern.
  • Tom Stambaugh
    Tom Stambaugh about 6 years
    It doesn't sound as though you're familiar with GoF or with mature Smalltalk implementations of Views and Models (the Smalltalk world left MVC behind by the middle 1980s -- see IBM Smalltalk for a better alternative). I have not proposed to wrap Flask actions and templates in classes. Calling the above "index.html" a "view" is like calling a hubcap a "car". A representative example of a modern web framework is Ruby On Rails (there are many others).
  • Zdeslav Vojkovic
    Zdeslav Vojkovic about 6 years
    Well, it might be ~20 years since I first read GoF book, but it still doesn't change the fact that MVC is not one of the GoF patterns, so it is completely irrelevant how familiar with it I am. Meaning of MVC pattern (which is not the same as Smalltalk MVC implementation) changed over time - otherwise no web MVC framework would fit your description. If you haven't proposed wrapping stuff in classes, I don't see how classes are then relevant. There is no essential difference between Flask templates and Rails "views" or between Flask methods and Rails 'controllers'.
  • Admin
    Admin about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.