Python Web Frameworks:
Django, Flask, Pyramid, Tornado, Bottle, Diesel, Pecan, Falcon
Flask:
Two external libraries:
Werkzeug Toolkit for WSGI (Standard Python Interface between Application and Server)
Jinja2 Template Engine (Renders templates)
Advantages:
- Easy to learn & implement
- Good documentation
Installation:
pip install Flask
Minimal Application:
from flask import Flask
app = Flask (__name__)
@app.route ('/')
def hello ('/'):
return 'Hello World!'
if __name__ == '__main__'
app.run ()
What we did above?
- Imported Flask class
- Created a single module instance '__name__'
- Used 'route' decorator to tell what url should trigger our 'hello' function
- Defined a 'hello' function (also helpful in generating the url later using url_for)
- Used 'run' function to run the local server
Debug Mode:
app.run (debug = True)
Url Building:
url_for( 'function_name' ) generates the url corresponding to the function_name
Url Methods:
@bp.route ( '/login', methods = [ 'POST', 'GET', 'DELETE', 'PUT', 'SEARCH' ]) and the like
Blueprint:
from flask import Blueprint
bp = Blueprint('bp', __name__)
Eg. app.register_blueprint(bp, url_prefix='/cat-ui')
#appends cat-ui prefix to all urls while generating them
url_for( 'bp.function_name' )
Why Blueprints?
- Supporting common patterns throughout the application
- Register default prefixes in application url
- Provide template filters, static files, etc.
Demo:
- Minimal Application
- Text box for sub-category change
- Overview of Product Catalog Tagging
Moving ahead with Pymongo:
- $addToSet (To append a new item in a list of value of a field)
- $group (Aggregates documents by desired key values)
- $match (Matches aggregated documents over a constraint)
- $project (Generates desired names for queries)
References:
http://flask.pocoo.org/docs/0.10/quickstart/#
http://opentechschool.github.io/python-flask/