Getting Started with Flask

What is Flask?

Flask is a MicroFramework for Python based on Werkzeug, Jinja 2 and good intentions.

What does it come with?

  • Built in development server and debugger
  • Integrated unit testing support
  • RESTful request dispatching
  • Uses Jinja2 templating
  • Support for secure cookies (client side sessions)
  • 100% WSGI 1.0 compliant
  • Unicode based
  • Extensively documented

Installation

Make sure you have a working Python Installation.

We will be installing Flask in a VirtualEnv(Virtual Environment) to make sure there is no conflict of dependency versions.

On Mac OSX and Linux Systems the following commands should be enough to get you up and running with virtualenv.

     sudo easy_install virtualenv
     
 OR
     sudo pip install virtualenv
     

Otherwise please follow these instructions to install Pip

Now to create a VirtualEnv

     #bash
     mkdir myproject
     cd myproject
     virtualenv venv
     New python executable in venv/bin/python
     Installing distribute............done.
    
    
Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:
    . venv/bin/activate
    
On Windows:
     venv\scripts\activate
    
And now you should finally be able to:
    pip install Flask
    

Runnning your first Flask App

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Welcome to Flask!"

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

Now open localhost:5000 to see the magic.