Flask

download Flask

If you can't read please download the document

description

Introduction to flask- micr

Transcript of Flask

* 1 * Flask - web development, one drop at a time http://www.flask.pocoo.org http://www.github.com/mitsuhiko/flask code by Armin Ronacher

* 2 * Flask? * Python Microframework * Based on Werkzeug, Jinja2 and good intentions * Jinja2 can be replaced by Mako or even JS templates. * lots of documentation * Was created with Small apps in minds.

* 3 * Installation? * pip install flask * 1964 watchers in github * 178281 downloads from PyPI * 4th most popular python repo in github

* 4 * What does micro mean? * Simplicity and small * Entire application in simple file * Core is simple but extensible * supports only python 2.5+

* 5 * Very Minimal application from flask import Flask app = Flask(__name__)

@app.route('/') def hello_world(): return "Hello world"

if __name__ == '__main__': app.run() * save file as hello.py, run python hello.py, head over to http://localhost:5000

* 6 * What is happening? * app is our instance of WSGI(Web Server Gateway Interface) * @ is decorator pattern, route() is url routing * Can pass host, port no also * app.run(host = '0.0.0.0')

* 7 * Debug mode * Flask has in built debugger( really awesome) * Very simple add app.debug = True or app.run(debug = True) * Doesn't work on real environment during deployment

* 8 * It looks like from flask import Flask app = Flask(__name__)

@app.route('/') def hello(): return 'Hello world'

if __name__ == '__main__': app.run(debug = True, host='0.0.0.0')

* 9 * Accepting parameters in URL @app.route('/user/') def print_username(username): return username

@app.route('/post/') def render_post(id): #show post pass

, ,

* 10 * Caution * Don't add ':' at end of the decorator @app.route('/'): => I make this very often Don't forget to add parameters in route() in function. @app.route('/post/') def render_post(): #yes it will throw error

* 11 * Beware! @app.route('/user') != @app.route('/user/') * Flask doesn't throw any error * When both url pattern flask will route one with trailing slash

* 12 * URL redirection from flask import redirect, url_for @app.route('/path/') def redirection(): return url_for('func_name') * You need to quote func name

* 13 * REST * Flask supports REST @app.route('/login/', methods=['POST', 'GET']) def login(): # Handle login procedure * If method allows only POST you can use GET method. * 14 * Jinja2 Templates * use render_template * create directory templates, file index.html in templates directory. {{ text }} save it.

from flask import render_template @app.route('/jinja/') def html(): return render_template(template.html, text = 'Flask is awesome')

* 15 * * Flask looks for templates folder, you can override - Find Out :) * request object is used to retrieve all details like methods, get parameters etc ... * request.method => yields http method like GET, POST, HEAD * request.form['attribute'] => get form attribute * Find out how to access request with formname * request.args.get('get_parameter')

* 16 * Connecting missing parts * Remember Flask is micro framework, You only WSGI and helpers. * Database connection ? * Sessions ? * Form handling ?

* 17 * Components * Flask will never have an ORM, form handlers, feel free to use your fav one * Database => Use SQLAlchemy, pewee, storm or xyz * Session => from flask import session session['username'] = 'value' * Remember sessions are stored in client side cookies. Large session details can be stored in redis or beaker :) * session is just python dictionary. * app.secret_key="this is used to encrypt all sessoin details"

* 18 * configuration * app.config has all config details * How to configure flask ? * app.config['parameter'] = value * app.config.update(DEBUG=True, version=1) * app.config.from_envvar('app settings') * 19 * Can I make large application * Yes you can use MVC pattern * You can use blueprints to modularize application * You can make blueprints for each subdomian or url * /admin/ can be one blueprint * /user/ can be one more blueprint

* 20 * Flask - extensions * Makes life simpler. * Helper functions to make interaction with 3rd party lib easier. * Few are Flask-admin, Flask-SQLAlchemy * Flask-cache, Flask-Celery * URL => http://flask.pocoo.org/extensions/

* 21 * Flask-snippets * How to do stuffs in Flask * URL => http://flask.pocoo.org/snippets/ * You can add your new snippets

* 22 * Community * Mailing List => [email protected] * IRC => #pocoo on freenode * lead developer => Armin Ronacher aka mitsuhiko * website => http://lucumr.pocoo.org

* 24 * Biased Recommendation - Sample projects * http://httpbin.org => https://github.com/kennethreitz/httpbin * http://kracekumar.com => https://github.com/kracekumar/Blaze * stallion => https://github.com/kracekumar/stallion * Gummi => https://github.com/kracekumar/Gummi * logmon => https://github.com/maxcountryman/logmon

* 25 * About me * Wanna Pythonista * twiiter: http://twitter.com/kracetheking * mail : [email protected] ***** Thank You *****