Working with Python Flask

Datagrepper is a web application that is build on the top of flask. Here, i am sharing with you how you can develop web applications in python using Flask framework.

I liked it because it was easy to learn and simple to use. Also many of you will have question why flask? And answer is

  • Extensive documentation
  • Easy to understand
  • Decoupled code
  • No ORM so you can use SQLAlchemy or storm
  • Support other templates like genshi, mako
  • Pocoo team
  • Testing support
  • Blueprint
  • Extensions
  • Module level integration
  • Code base small you can check the source code.
  • 100% WSGI compliant.
  • Integration with gevent, twisted, tornado is possible.
  • Useful snippets in the pocoo site.
  • You can deploy with cherrypy.
  • No inbuilt form so you can use wtforms or any other.
  • Code by Armin Ronacher 🙂
  • Supports REST

So, to dive into code, First you will have to do the installation.

First install virtualenv. virtualenv will let you create an isolated environment for your project with its own installation directories, will let you install all dependencies into the virtualenv without affecting the system site-packages.
$ sudo easy_install virtualenv
$ sudo pip install virtualenv
# Mac OS X or Linux
$ sudo apt-get install python-virtualenv 
# ubuntu

After installing virtualenv, you will have to create your environment by creating a folder. 

$ mkdir flask-learn
$ cd flask-learn
$ mkvirtualenv flask
$ workon flask
$ pip install Flask

Now, we will have to create directory structure like way.

$ mkdir app
$ cd app
$ mkdir static
# holds all css, JavaScript files
$ mkdir templates
# store web app templates
My first Hello World program in flask
from flask import Flask
app = Flask(__name__)
def hello_world():
    return 'Hello, World!'

if  __name__ == '__main__':

Save this program as .Now run it.

$ python
* Running on - - [03/Nov/2013 23:28:42] "GET / HTTP/1.1" 200 -

Open http://localhost:5000 to see the results.

To learn more, you can refer this link.
Now, lets do add some html file.
$ cd templates
$ vi layout.html

add the following lines to layout.html

<!DOCTYPE html>
        <h2><font color="red">Happy Learning! </font></h2>

    {% block content %}
      <h3> This is the home page for Flask App</h3>
    {% endblock %}


Change the to this.

from flask import Flask, render_template
app = Flask(__name__)

def hello_world():
    return render_template('layout.html')

if __name__ == '__main__':

Now, run


You will get output 🙂 like this


To gain a good knowledge in flask and to try writing some codes on it- read this tutorial. It will provide you road-map for various web-app development using flask.

I will be updating more about database models, various methods, error handling etc. in future blogs. till then

Happy Coding!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s