Flask or Django? Which to Choose for your Project

🔖 code  django  flask  python 

Often I get asked by fellow python developers why I chose Django/Flask for a particular project (usually by someone who prefers the framework I didn't choose 😉). I think both frameworks are excellent and are well suited for a variety of use cases.

So how do I decide which to use for a new project? I found a simple heuristic to get 90% of the way to a final decision, and it's pretty easy to follow:


Essential Django Apps for Every Project

🔖 code  django  python 

Django projects have the ability to install apps, which are analogous to plugins in other frameworks.

Some of these apps provide simple functionality: django-gravatar installs a template tag for displaying a user's gravatar in a template. Other apps are large, like Mezzanine which provides an entire CMS framework to your project.

No matter what you are building, you should consider the following apps. I use them in almost all of my projects.


A Not so Dramatiq Change: A Celery Alternative

🔖 code  astronomy 
Both Celery and Dramatiq are asynchronous task processing libraries. You'd use them when you want to be able to parallelize Python code, and you need more than the multiprocess module offers, like persistent distributes queues, automatic retries, and result handling. I've been using Celery for almost my entire career, and it's treated me well. Recently I've started to become frustrated with it. There have been numerous regressions that have broken my code, as well as some totally inexplicable issues in the last few months (that last one is the reason I started looking for alternatives). Read more...

Line by Line Simple but Usable VIM Config

🔖 code 

VIM is a great editor, but it's defaults are a little lacking. Fortunately it's also extremely configurable. This leads many people (myself included), to scour the internet for lines of internet wisdom to copy in paste into their .vimrc files until they get something that works for them. Before you know it you have 300 lines of unintelligible gobblegook. In this post, (which I've started writing in vanilla vim) I'm going to go line by line through individual config items to construct a simple but usable .vimrc without too much magic or frills.


GNOME Notifications for Remote Weechat

🔖 code 

I ❤ Weechat. It's my IRC client of choice. But I also use it for gtalk and Slack. All my conversations in one convenient interface. Even better, I run it in a remote tmux session so I can pick up wherever I left off from anywhere.

The only annoying thing about this setup was the lack of real notifications for private messages or mentions. So I wrote Weelisten.


Weelisten is a small python script that leverages Weechat's relay protocol, python 3 asyncio and libnotify so I can get awesome native notifications on my desktop.

Sounds useful? Get it on Github

Throttling Specific Actions in Django Rest Framework Viewsets

🔖 code 

If you are using rate limiting with Django Rest Framework you probably already know that it provides some pretty simple methods for setting global rate limits using DEFAULT_THROTTLE_RATES. You can also set rate limits for specific views using the throttle_classes property on class-based views or the @throttle_classes decorator for function based views.

What if you are using ViewSets but want different throttling rules to apply to different actions? Unfortunately DRF provides no official method of doing this. Luckily we can accomplish this functionality without too much fuss using get_throttles().


Dockerize! Lest you forget

🔖 code 

I host quite a few sideprojects on my VPS. They range from static Jekyll sites (like this one) to large web applications. There's even some wordpress hiding in a corner, disgraced and neglected.

Despite the fact that none of these sites are actually useful for anything, they still need some poor bastard to keep then running. Over the years I've collected quite the assortment of nginx, uwsgi, php, apache, supervisor, and other configs. All of them written at various levels of understanding, none of them tracked anywhere, all of them confusing and terrible.


Preserve GET parameters using django-bootstrap3 pagination

🔖 code 

This one got me for a bit. If you are using django-bootstrap3 and also want to use it's handy bootstrap_pagination template tag for generating pagination links, you may be in for an unplesant surprise if your view uses any GET parameters. While the django-bootstrap-pagination project handles this by default, django-bootstrap3 will not persist GET paramters between pages.

The key to using the bootstrap_pagination tag is the extra argument, which takes a string and appends it to each page. If you have the request_context context processor installed, you can pass in this string using the QueryDict urlencode() method. For example:

{% bootstrap_pagination page_obj extra=request.GET.urlencode %}

Voila. Pagination in django with bootstrap working as it should.