Flask for cs students

4,126
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,126
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Flask for cs students

  1. 1. Building Web Applications With Flask: For CS Students By Jennifer Rubinovitz @rubinovitz
  2. 2. Why Flask? ● lightweight ● python ● (relatively) easy to deploy
  3. 3. Python: as a language ● ● ● ● Clear syntax Object oriented Great community and packages Fast (enough). Compiles to C
  4. 4. Python: Hello World print ‘hello world’ python 2.* print(“hello world”) python 3.*
  5. 5. Python: Recommended Resources ● ● ● ● Learn Python the Hard Way Python Documentation Codeacademy Flask Documentation
  6. 6. Installing Python Differs based on operating system http://www.python. org/getit/ Downloads:
  7. 7. 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
  8. 8. 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
  9. 9. Model-View-Controller(MVC)
  10. 10. 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)
  11. 11. Routing: unique url rules @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello World' Flask routing documentation
  12. 12. 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
  13. 13. Routing: HTTP @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form()
  14. 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. 15. 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
  16. 16. 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()
  17. 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. 18. Deployment ● Heroku ● Appfog ● Or deploy on your own server
  19. 19. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×