Sunday, February 20, 2011

First Impression: Grails vs Flask

Try this Exercise
Give yourself 2-3 days, pick up a new web framework you never use before, and build a small blogging app. Well, that's how I got first introduced to these two frameworks: Flask and Grails.

With short time constraint, it forces me to be aware of which parts of a framework actually help (or hinder) a developer to make progress. (btw, thanks to Sooyoung who introduced me the exercise ;-)

Here's a couple of things I observed while using Flask and Grails:
(Disclaimer: the post is written solely based on my first-time experience messing with the frameworks in few days time, so take it easy :-)  


Source Folder Structure
  • Flask lets you decide the source folder structure. You can code everything in single .py file, or organize by modules. It took me couple hours just to re-organize my single .py file to a working module structure. Also, you have to worry about how to organize unit tests.
  • Grails manages all folder structures for you. It provides commands (grails create-*) to create source files for you (e.g. controllers, views, unit tests) and store them properly in separate folders. A big big time-saver! 


Domain Model
  • It's okay to live without domain models in Flask. You can use SQLite, and switch to SQLAlchemy when really need it.
  • Grails has a rich Model DSL. The DSL lets you define model fields, relationship between models, processing before/after persistence, and input validation rules in single Groovy class file. Pretty neat stuff. After that, you can use GORM dynamic methods to query records. Here's an example:
    class User {
        String username
        String password

        static hasMany = [entries: Entry]

        static constraints = {
            username(blank:false, size:3..12)
        }

        def beforeInsert = {
            password = password.encodeAsSHA256()
        }

     
    // GORM dynamic finder:
    // def user = User.findByUsernameAndPassword(
    //                   username,
    //                   password.encodeAsSHA256())

Controllers
  • Flask and Grails both provide common controller utility objects (e.g. session, request) and functions (e.g. render, redirect, forward). I guess it's pretty much a standard feature now across modern web frameworks.
  • URL routing in Flask is annotation-based (similar to JAX-RS), while Grails separates it out to a config file UrlMapping.groovy. Interestingly, this led me to design URL structure up-front in Flask, while Grails allows me to delay the decision until the end of development.


View
  • Flask uses Jinja2 templating engine. I like Jinja2 for two reasons. First, the syntax is clean. Second, it provides an elegant model to extend base template. It's pretty much same as how a class method overrides its parent method:
    // base.html
    <html>
    <head>
      {% block head %}
      <link rel="stylesheet" href="main.css"/>
      {% endblock %}
    </head>
    <body>
      {% block navbar %}
      <div id="navbar"> .. </div>
      {% endblock %}
    </body>
    </html>

    // login.html
    {% extends base.html %}
    {% block head %}
      {{ super() }}
      <link rel="stylesheet" href="login.css"/>
    {% endblock %}
    {% block navbar %}
      {# empty block to hide navbar #}
    {% endblock %}

  • Grails uses GSP + SiteMesh templating engine. The XML-based DSL is a bit less elegant. Took me quite a while to figure out how to render the same output as Jinja2:
    // base.gsp
    <html>
    <head>
      <link rel="stylesheet" href="main.css"/>
      <g:layoutHead />
    </head>
    <body>
      <g:pageProperty name="page.navbar"
         default="${render(template:'navbar')}"/>
    </body>
    </html>

    //_navbar.gsp
    <div id="navbar">..</div>

    //login.gsp
    <head>
      <link rel="stylesheet" href="login.css"/>
    </head>
    <content tag="navbar">
      <%-- empty block to hide navbar --%>
    </content>

Dev Tools
  • Flask and Grails both apply code changes automatically when source files modified during development. I think it's a must-have feature. It enables developers to code without restart web server manually to apply changes.
  • In addition, Grails comes with a set of dev tools, such as bootstrap test data, config files, grails console etc.

If using the framework in next project..
I would use Flask for prototyping, as I can quickly code the entire app in single python file and run it. However, for a complex web app, Flask requires extra efforts. In that case, I might consider to use Grails.  

Anyway, if using Flask in my next project, I would invest initial efforts to..
  • setup complete source folder structure,
  • add config files,
  • find a input validation framework,
  • a web security plugin,
  • and i18n support.
For Grails, I might look for a way to simplify its templating DSL (using taglib?), and try out some web security plugins.


Try out Flask or Grails?
Feeling itch? Here's the kick-start tutorials:

    No comments:

    Post a Comment