Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

Python Flask Tutorial

Flask is a popular Python web framework that is easy to learn and implement. It has good documentation and requires only two external libraries: Werkzeug for the WSGI interface and Jinja2 for templating. A minimal Flask app imports the Flask class, creates an app instance, defines a route decorator for the view function, and runs the local development server. Blueprints allow supporting common patterns, registering URL prefixes, and providing additional resources like templates and static files. The document then provides an example of a minimal Flask app and discusses url_for, view methods, and blueprints before moving on to introduce Pymongo for working with MongoDB.

Uploaded by

Shashank Dixena
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Python Flask Tutorial

Flask is a popular Python web framework that is easy to learn and implement. It has good documentation and requires only two external libraries: Werkzeug for the WSGI interface and Jinja2 for templating. A minimal Flask app imports the Flask class, creates an app instance, defines a route decorator for the view function, and runs the local development server. Blueprints allow supporting common patterns, registering URL prefixes, and providing additional resources like templates and static files. The document then provides an example of a minimal Flask app and discusses url_for, view methods, and blueprints before moving on to introduce Pymongo for working with MongoDB.

Uploaded by

Shashank Dixena
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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/

You might also like