Thoughts on Flask, Python and Node.js

Recently I’ve been working on a software design competition held in my university. This year’s theme has something to do with GPS/LBS application, which requires candidates to use designated toolkits provided by the sponsor. Since the toolkits is not mature, my team decided to focus on the other option: anything as long as it’s attractive because it’s not mandatory to conform to the theme.

Anyway, we decided to write an app which enables people to draw their interactive models. Since I have experience in former CMS work, I am in charge of server this time. In CMS, I used Node.js and Express for all of the development so I’d like to try something new, like Python, in this project.

Above is the prelude of my thoughts. Initially I set up a virtual environment using pyenv and pyenv-virutualenv. Compared to Node.js ’s NPM, Python’s PIP is not that user-friendly.

PIP is not an official requirement for Python development. Popular as it is, PIP is still faced up with some alternatives including easy-install. However, NPM is a must-have tool for Node.jsers and it provides with convenient module management, no matter local and global.

After the environment configuration, I chose flask as the web structure according to its popularity and well-known pythonic style of apis. As is mentioned before, I used Express when I worked on CMS using Node.js. Express is comprehensive but not complicated. It’s as powerful as Django for Python but does not introduce many new concepts so JSers, as long as they’ve been familiar with async IO, would fall in love with it for they can start building web applications in several hours without too much effort learning Express.

When it comes to Flask, things are really different. If one is accustomed to Javascript, he may feel free to write a {} to represent an object. Object is everything. Anything can be object. These two sentences seem right in JavaScript’s world but we had better be good boys in Python. Python is simple and rigid, to some extent. Dict is a primary data structure in Python but in Javascript, {key: value} is merely an object. Take json as an example, there is always conversion before you use it in Python but json has nothing different with another common object except json’s quotation marks.

What follows is callback. In JavaScript, Every IO is async. There is no exception. Thus programmers write numerous callbacks or invent something new to mimic the synchronizing way. Therefore it’s very natural in Express to register route controllers using callbacks. In Flask, the decorator is introduced and adopted as the solution.
A decorator is something like

1
func = decorator(func)

Closure is used here to wrap the original func with something added and return the wrapper to replace with the original variable : func. It’s great fun in that it enables programmers to re-use and decouple functions.

What has confused me for one or two days is the module import mechanism of Python. In Express (Node.js) or Django (Python), relationships of different modules have been controlled by the structure itself and everything is fine. Flask is light, which means users need to configure something themselves . If you use blueprint, things will be better but if not, the knowledge of module import is a life buoy when you encounter errors. In Flask, import loop should be avoided and this prompts programmers to think more on the structure of his codes.

Comments