26

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
2
  • 1
    May also look into this suggestion. github.com/salimane/flask-mvc Commented Aug 20, 2015 at 15:12
  • 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. Commented Aug 4, 2017 at 10:32

3 Answers 3

30

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

Sign up to request clarification or add additional context in comments.

7 Comments

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.
"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.
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.
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.
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'.
|
4

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"

Comments

1

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

2 Comments

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.
Sorry but what you call a controller is actually view and what you call a view is a template. There is no controllers in Flask as the routing is done by the WSGI/Flask request_handler which will match the URL and pass to the view decorated by that URL. Request sent to the view, view handles both model and template/response. More like MVT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.