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.

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
or
$ 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__)
@app.route('/')
def hello_world():
    return 'Hello, World!'

if  __name__ == '__main__':
    app.run()

Save this program as hello_world.py .Now run it.

$ python hello_world.py
* Running on http://127.0.0.1:5000/
127.0.0.1 - - [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>
  <html>
  <head>
    <h1>Flask</h1>
  </head>
  <body>
    <header>
      <div>
        <h2><font color="red">Happy Learning! </font></h2>
      </div>
    </header>

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

  </body>
</html>

 
Change the hello_world.py to this.

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

@app.route('/')
def hello_world():
    return render_template('layout.html')

if __name__ == '__main__':
    app.run()

Now, run hello_world.py

 

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!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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