A Quick Sip from the Flask Framework
#PyTN2015
slides
goo.gl/67RrXH
slides
goo.gl/67RrXH
repo
ptyn-flask-demo
Andy Matthews
@commadelimited
andyMatthews.net
Adobe
Packt
Nettuts
Eventbrite
What's Flask?
Created by Armin Ronacher in 2010
Web-based Python microframework
Flask core is simple and extensible
What's included?
Templating, Sessions
Blueprints, Message Flashing
Route based URLs

@app.route("/")
def hello():
    return "Hello World!"
    
Signals
Best parts of convention and configuration
What's missing?
ORM, Forms
Login, Admin
Flask Quick start
Easy to explain
As simple as a single file: app.py
step_01
and
step_02
Modularization
Easy to extend
Start small, add as you need
Blueprints
step_03
The Flask API
The Flask lifecycle
Create app context
Create request context
Do magic
Destroy request context
Destroy app context
g

from flask import g
g.nash = 'vegas'
g.get('nash', None)
    
before_request

from flask import g
from flask_login import current_user
@app.before_request
def before_request():
    g.user = current_user
    
request

from flask import (request)
request.method (get/post)
request.args.get('page')
request.endpoint
> 'news.show_news'
    
route()

from flask import (url_for, redirect)
url_for('news.show_news')
> '/news/'
return redirect(url_for('foo.bar'))
    
Flashing

from flask import (flash)
flash('Sweet mustache bro!')
    

{% with messages = get_flashed_messages() %}
    {% for message in messages %}
        <span>{{ message }}</span>
    {% endfor %}
{% endwith %}
    

<span>Sweet mustache bro!</span>
    
But what about templating?
Jinja2
Straight up awesome!
Plain HTML
with extras
{{ variable }}
{% expression %}
Request context aware

{{g.user.id}}
    
Template inheritance
{% extends %}
{% block %}
{% endblock %}
Filters
number | round
str | capitalize
str | escape
str | replace("Hi", "Bye")
Lots more
Custom filters too
Macros
Like a function, outputs html
And that stuff Flask doesn't do?
No ORM
Flask-SQLAlchemy
Flask-Peewee
No Forms
Flask-WTF
No Login
Flask-Login
No Admin
Flask-Admin
47 others
Questions?
Comments?
Snide Remarks?