Sunday, January 13, 2013

Code Review: githubsurvivor -- Python web application written in Flask

"GitHub Survivor is a simple bug dashboard that shows an overview of bugs in a GitHub-hosted repo. We use it at 99designs to keep an eye on the bug count and remind ourselves to close bugs."
Interesting concept, nice charts and ability to infer state of git repos by mere glance at dashboard. With Fafadia Tech's continued passion and linking for both Flask and Python, this writeup serves to give you under the hood look at the code with 99 designs open-sourced it about 3 months ago.


  • First quick look at dependencies and packages this project uses:
    • mongoengine==0.7.3
    • Flask==0.9
    • PyGithub==1.6
    • python-dateutil==2.1
  • Apart from now standard Flask + MongoDB setup, this project uses Python wrappers for github APIs.
  • Less is used to for generating css files 
  • Authors have used a Make file with all and cleanup options used to compile from less syntax to css style sheets
  • All static assets is stored in resources folder called res
  • Project configuration is present is external python file: config.example.py (obviously this is just for reference)
  • Jinja2 templating engine is used. All templates exists in templates folder 
  • Template inheritance and block are amongst some Jinja features that are used. Look at base.jinja2 which is extended in most of other templates. 
  • bin directory contains command for setting up, serving and running tasks
  • MVC pattern is followed for organizing code. models folder contains one python file per model, containing document definitions and method calls 
  • sync.py file contains all logic for syncing data from github along with some other helper methods
  • Main webapp logic is written in __init__.py file under web directory 
  • reporting.py file is used for reporting purposes and timeutils.py for handing timezone, date formatting etc

Overall the code is minimalistic and clean. A good reference to learn from.

Thursday, January 3, 2013

Tutorial: Deploy Django app on AppFog


AppFog is PaaS provider which make management of code a little easier. There are bunch of PasS providers out there Heroku, Elastic Beanstalk to name a few. I tried Googling easy to use tutorial for deploying Django app on AppFog couldn't find any so following article is compilication of what has worked for me based on trial and errors.

Before delving into details of the application, there are few pre-requisites we assume

1. You must have AppFog account.
2. You must have virtualenv installed on development machine. Pointer that might help you.
3. You mush have af gem installed locally. Instructions to install.

Optionally you can also you Git hub repo, for version controlling your code. You can find sample code for this tutorial on GitHub.

Steps for Deploying Django app on AppFog

1. Create a New App

2. Select Python Django framework

3. Choose infrastructure Region

4. Wait till the App is deployed

5. Download Source Code

   We need to follow this step because AppFog gives us Jumpstart code with all pre-built configuration incluing but not limited to DB settings.

6. Create Virtual Env and install dependencies

  • virtualenv ~/Installs/env/django-appfog
  • source ~/Installs/env/django-appfog/bin/activate

   This is optional so that we can test our work locally.

7. Update settings and created static files

  • Create directory called static at same level as manage.py file. Run 
  • cd static/
  • python ../manage.py collectstatic
  • Open up setting.py file and import settings
  • Add and change ROOT_PATH = os.path.dirname(__file__) and STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
  • Add following line to urls.py url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

   By default Django doesn't serve static in production, so we need to create separate static folder and put all necessary files. In our case we're going to be checking admin page to see if our static is getting served.

8. Update the Application

  • af update django-appfog
Note: django-appfog is our application name for this tutorial purpose. If you've created a different application name, be sure to use right value. 


The other thing I couldn't figure out was to admin username and password for newly created apps. But couple of posts suggest doing it programmatic or creating an entry in DB, which to be honest for a PaaS solution seems little lame.

Found better solution? Do let us know in comments