Chapter 1. Flask in a Flask, I Mean, Book

What is Flask? It's a question that humanity has been pondering for millennia... well, actually, since 2010, when Armin Ronacher first committed to the project. Flask is a Web framework and is quite different from what most people are used to working with. It is less presumptuous about how your application should look or what you should use to make it available. The BSD licensed package has all this!

An introduction to Flask and its features

The Flask framework is actually a glue, a very nice one, that sticks together the amazing Werkzeug and Jinja2 frameworks, responsible for answering requests and presenting the output (HTML, maybe). In the MVC architecture, also known as Model-View-Controller, Flask covers C and V. But where is M? Flask does not provide you with an integrated model layer out-of-the-box as that is not actually needed for a web application. If you do need to work with a database, just pick your database solution from the many available and create your own model layer, which is not hard, and be happy! The concept of a micro-framework, with good intentions and made just for Flask, is all about giving you the the smallest (but also the most useful) feature set you need, and one that won't get in the way.

Just what are the features that must be in the framework?

  • A development server and debugger (sanity-friendly)
  • Unicode support (Latin language-friendly)
  • WSGI compliance (uWsgi-friendly)
  • A unit-test client client (code with quality)
  • URL routing (it brings tears to my eyes, it's so beautiful!)
  • Request dispatching
  • Secure cookies
  • Sessions
  • Jinja2 templates (tags, filters, macros, and more)

With that much, you can handle Ajax requests, browser requests, and user sessions between requests; route HTTP requests to your controllers; evaluate form data; respond to HTML and JSON; and so on.

That is nice, but is Flask not an MVC framework? Well, that's arguable. If a web framework does not implement an MVC antipattern, such as handling requests in the view or mixing models and controllers, it could potentially facilitate an MVC, which, in my opinion, is as good as it gets because it does not enforce your application structure.

Note

Flask is not an MVC framework as it does not implement the model layer, although it does not restrict you in any way if you wish to create your own.

If you need a simple, single-file web application that receives a form and gives back an answer, HTML or not, Flask will help you with that, easily. If you need a multilayer, high-depth modularized Facebook clone, Flask can also be there for you.

So, what did we learn so far?

  • Flask was born in 2010
  • Flask is a minimalistic web framework based on Jinja2 and Werkzeug
  • Flask does not enforce a specific project architecture

Note

Refer to Flask license details at http://flask.pocoo.org/docs/0.10/license/.

Right now, you might be wondering which of your neat project ideas could be done with Flask. That's the spirit! What about thinking together on this one?

Flask does not come with bundled functionality in terms of database integration, a forms library, administration interface, or migration tools. You can have these through extensions, which will be discussed soon enough, but they are all external to Flask. If you need these extensions right at the beginning of your project and you don't want to set it up (or can't spare the time to), you might do better with a full-fledged MVC all-in one, low-cohesive, and high-coupling framework such as Django.

Now, imagine you need to build a website with a single form, such as a http://cashcash.cc/ clone, which receives a form and returns the current currency trade values; Flask could help conclude your project really fast.

Let's think further. What if you need a specific set of libraries to work together in your project and you don't want the web framework getting in the way; that's another very good scenario for Flask as it gives you the bare minimum and lets you put together everything else you may need. Some frameworks have such a high level of coupling (read dependency) on their own components that you may have a serious problem if you want to use a specific alternative.

For example, you may want to use a NoSQL database in your project; however, if you do so, some components of your project may stop working (for example: an administrative component).

Basically, if you have the time to spare, if you're doing something simple, if you want to implement your own architecture solution, or if you need granular control of the components used in your project, Flask is the web framework for you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.185.147