Archive for August, 2015


Python: Flask vs Django

Python is everywhere!
The place where it is really starting to grow and dominate is the web.
Thanks to the development of the web framework Django, Python has become a big competitor.
But since it’s release, a few other players have entered the market.
Most notably Flask, and Pyramid.

Django is often described as a web framework with batteries included.
And Flask is described as the micro web framework.

Now, I have used both, and in all honesty it’s very clear that Django is the most popular.
But sometimes I really have to ask myself why.
My personal preference lies with Flask over Django.
And let me explain why.

A lot of times, when a framework becomes popular, developers who were at home with things like PHP will learn about the frameworks. In this case, Django.
Whilst in essence Django is a Python framework, it does things slightly differently, and knowledge about Django isn’t always about needing to know much Python. Flask on the other hand acts more like an add-on to Python than it does as a stand alone framework.
I have often had a bit of a debate about Django vs Flask with ‘Djangonauts’ (yeah, I know, I feel sorry for them too).
And there seems to be a lot of misunderstanding.

The following is what a Django developer will tell you why you should choose Django over Flask:

  • Django has an admin page, which Flask doesn’t
  • Django is a batteries included framework
  • With Django you can easily make large apps which fit together, Flask is for small projects.
  • Django has an amazing ORM which Flask doesn’t

Well, that sounds a very strong argument. If it was entirely true obviously.

It is true that Django has an admin page which can be turned on and is ready to go.
Whilst Flask doesn’t have that built in, thanks to Flask-Superadmin which is installable using Pip, you have the same functionality with Flask without writing lots of code. Sure, you will need to add it yourself, but I’ll explain later why I think it’s worth it.
Django is indeed an all batteries included framework and Flask is not, but that isn’t always amazing. If you are writing a simple blog, you may be working with a huge framework which isn’t necessary.

The third argument in my opinion (and perhaps mine alone) shows a lack of understanding of Python.
The notion that Flask cannot build different apps linked together means you do not understand how to make modules in Python. Make a new folder, make an empty __init__.py file, and your app file and you have an independant app.
Then call that app from the root directory in a main python file and it’s done. There is basically nothing stopping you from building a large scale project with multiple apps in Flask. The only difference is that in Flask you make those files by hand, and in Django it is automatically generated.

And the Django ORM is great, there is no denying that. But, there is an almost identical ORM for Flask which many are familiar with called SQLAlchemy, which can be used with the package Flask-SQLAlchemy.

An often not mentioned argument is the long term maintenance of a project.
Thanks to the complexity of Django, long term projects are a nightmare.
Dependencies, the not overly Pythonic way of programming all end up meaning with upscaling and growth, you end up having to change code all over the place.
Because in essence Flask is very much a Python project, if you program it well, it means long term projects are easily maintainable.

But for me the biggest plague is the routing of URL’s.
Django uses Regular Expressions. Which, whilst useful for many things, is not the ideal way to be routing.
And on top of that, the routes are in different files to the actual code being called.
So it works like this:

A user goes to ‘/’…this is looked for in the urls file until found which refers to a model…so it goes to the model it referred to and runs the code.
Doesn’t seem too bad, but changing one route, or adding it means you have to change at least two files.

Flask does it different, it firstly doesn’t use Regular Expressions. It simply uses a line like this:
@app.route(‘/’)

Seems simple right? There in the brackets is the route.
But even better is that the code that is to be run for that route is directly underneath it.
So this may be a complete route and function:

@app.route(‘/’)
def home_page():
render_template(‘home.html’)

That’s it.
All it does is check which route is being requested, goes to the function directly beneath it, and with the render_template function it gives back the home.html page for the user to see.

And a project can look complicated, or be a single .py file.

So in my opinion, Django is overrated and bloated. Try Flask!

Advertisements

I want to rant a little.
No hate though, but something that slightly irritates me, and perhaps you can relate to.

Being a member of the Python and Linux community on Google+, I see a lot of new users come and ask for advice.
One question asked, and it’s responses really sums up my irritation:

Question: ‘I am new to Python and Linux, and would like to start learning. Which version of Python should I use, and should I use Ubuntu or CentOS?’

Well that’s an easy question right.
Let’s look at what the poster told us about himself. He is NEW to BOTH Linux and Python.
So what we ought to suggest is something perfect for a new user.

For those not in the know, Python is currently split between Python 2.7, and Python 3.x.
The reason for this is that there are such big differences when Python 3 was introduced, that a lot of third party libraries and extensions no longer work. As a result, a lot stuck with Python 2.7 for that support.

But considering this user isn’t dependent on third-party libraries (he is new), we should suggest the newest version 3. After all, there will come a time we all must switch.

Then there is the Linux question.
I am a long time (12+ years) Linux user having used pretty much every distro.
Currently a Fedora user, which shares it’s core base with CentOS, I can say without doubt that Ubuntu is the most easy to use distro, with a fantastic community if a new user get’s stuck.

Well, it did not take long or the subject of IDE’s was introduced.
I’m a Python developer, and I always recommended PyCharm. After all, it’s a modern IDE specifically built for Python, but with plugins for other languages. I even liked it better than Sublime.

Then this really annoying phenomena happened.
In what I can only imagine is a means of self-boasting, and trying to be seen as some kind of master of Linux, the following suggestions started to appear for a new user:

Answer:’I recommend using Arch Linux because it is so robust and rolling release. And the IDE you want to use is VIM’

I can’t really swear on here, but needless to say, my head exploded.
Who in their right mind can recommend a new user to Linux to use the most difficult to set up Linux distrobution, which relies heavily on the Terminal? A new user has most likely never touched the terminal, and you are suggesting he use a terminal for EVERYTHING.
And to top it all off, suggesting he use the most difficult editor available?

I have worked on VIM quite a bit (Fedora doesn’t come with nano preinstalled), but I personally rather install Nano.
You may love VIM, but let’s be honest, it’s far too old to be taken serious as a development environment when we live in an age of GUI’s.

And that is exactly what is wrong with the Linux community.
More and more relatively new users want to feel superior by using distro’s like Arch Linux, just for the 1337 factor.
That’s all fine, Arch is a good distro (I’ve used it too), but for more advanced users.
Suggesting really difficult OS’s, and tools to new users is only to try and make yourself seem more impressive than you really are.

So let’s do ourselves, and those asking for help a favour:
Let’s take THEM as the case file. After all, they have to use the tools we provide, not yourself.
Place yourselves in their shoes. Bring yourself back to the first time you used Linux, would you rather have been using Arch and VIM, knowing it may well put you off forever, or use Ubuntu and a modern IDE built for a specific job?