Building Web
Applications With Flask:
For CS Students
By Jennifer Rubinovitz
@rubinovitz
Why Flask?
● lightweight
● python
● (relatively) easy to deploy
Python: as a language
●
●
●
●

Clear syntax
Object oriented
Great community and packages
Fast (enough). Compiles to C
Python: Hello World
print ‘hello world’

python 2.*

print(“hello world”)

python 3.*
Python: Recommended Resources
●
●
●
●

Learn Python the Hard Way
Python Documentation
Codeacademy
Flask Documentation
Installing Python
Differs based on operating system

http://www.python.
org/getit/
Downloads:
Python Package Management
PIP:
Allows you to download python packages on
the fly:
http://www.pip-installer.org/en/latest/installing.html

pip install packagename==versionNumber
Virtualenv
Creates isolated python environments and
install an applications requirements
Command line
virtualenv venv
. venv/bin/activate
pip install -r requirements.
txt

requirements.txt
flask==0.10.1
Model-View-Controller(MVC)
Hello World: simple_server.py
import os
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True, port=8000)
Routing: unique url rules
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello World'

Flask routing documentation
Routing: variable urls
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
Routing: HTTP
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
do_the_login()
else:
show_the_login_form()
Templates: Jinga
{% extends "layout.html" %}
{% block body %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}

Jinga Documentation
Models: classes
Models are abstractions of database tables
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
Models: operations
# create a user
jen = User(username=”jen”, email=”jen@email.
com”)
# save user to database
db.session.add(jen)
db.session.commit()
# retrieve user
jen =db.session.query.filter_by(username=”jen”).
first()
Favorite Flask Modules
● Flask-Sqlalchemy(for database interaction)
note: flask-sqlalchemy is a sqlalchemy wrapper for flask, and their syntax
differs. Make sure you use the correct documentation.

● Flask-Security (for user management)
● WTForms(for forms)
Deployment
● Heroku
● Appfog
● Or deploy on your own server
Questions?

Flask for cs students

  • 1.
    Building Web Applications WithFlask: For CS Students By Jennifer Rubinovitz @rubinovitz
  • 2.
    Why Flask? ● lightweight ●python ● (relatively) easy to deploy
  • 3.
    Python: as alanguage ● ● ● ● Clear syntax Object oriented Great community and packages Fast (enough). Compiles to C
  • 4.
    Python: Hello World print‘hello world’ python 2.* print(“hello world”) python 3.*
  • 5.
    Python: Recommended Resources ● ● ● ● LearnPython the Hard Way Python Documentation Codeacademy Flask Documentation
  • 6.
    Installing Python Differs basedon operating system http://www.python. org/getit/ Downloads:
  • 7.
    Python Package Management PIP: Allowsyou to download python packages on the fly: http://www.pip-installer.org/en/latest/installing.html pip install packagename==versionNumber
  • 8.
    Virtualenv Creates isolated pythonenvironments and install an applications requirements Command line virtualenv venv . venv/bin/activate pip install -r requirements. txt requirements.txt flask==0.10.1
  • 9.
  • 10.
    Hello World: simple_server.py importos from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template("index.html") if __name__ == '__main__': app.run(debug=True, port=8000)
  • 11.
    Routing: unique urlrules @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello World' Flask routing documentation
  • 12.
    Routing: variable urls @app.route('/user/<username>') defshow_user_profile(username): # show the user profile for that user return 'User %s' % username @app.route('/post/<int:post_id>') def show_post(post_id): # show the post with the given id, the id is an integer return 'Post %d' % post_id
  • 13.
    Routing: HTTP @app.route('/login', methods=['GET','POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form()
  • 14.
    Templates: Jinga {% extends"layout.html" %} {% block body %} <ul> {% for user in users %} <li><a href="{{ user.url }}">{{ user.username }}</a></li> {% endfor %} </ul> {% endblock %} Jinga Documentation
  • 15.
    Models: classes Models areabstractions of database tables from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) def __init__(self, username, email): self.username = username self.email = email
  • 16.
    Models: operations # createa user jen = User(username=”jen”, email=”jen@email. com”) # save user to database db.session.add(jen) db.session.commit() # retrieve user jen =db.session.query.filter_by(username=”jen”). first()
  • 17.
    Favorite Flask Modules ●Flask-Sqlalchemy(for database interaction) note: flask-sqlalchemy is a sqlalchemy wrapper for flask, and their syntax differs. Make sure you use the correct documentation. ● Flask-Security (for user management) ● WTForms(for forms)
  • 18.
    Deployment ● Heroku ● Appfog ●Or deploy on your own server
  • 19.