Flask is a very small framework for eveloping web apps.
Create the following directory structure:
- routes.py
- templates
- static- css
- img
- js
 
- Readme.md Flask relies on Jinja2 templates. These look like:
{% extends "layout.html" %}
{% block content %}
  <main class="hero-section">
  </main>
{% endblock %}routes.py contains the main dispatcher:
brush:python
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
  return render_template("index.html")
@app.route("/about")
def about():
  return render_template("about.html")
if __name__ == "__main__":
  app.run(debug=True)To install flask it is best to use a VirtualEnv. Download from the web and create a new virtual environment:
python virtualenv.py my_veTo activate or deactivate th virtual environment:
source my_ve/bin/activate
source my_ve/bin/deactivateTo install flask in the VE:
pip install flaskTo deploy to heroku:
- Install heroku CLI
- Install gunicorn web server
pip install gunicorn- Create a requirements.txt - pip freeze >requirements.txt 
- Create a Procfile to tell Heroku to run flask in gunicorn - web:gunicorn routes:app 
- Create a heroku app - heroku create 
- We can now push to Heroku git repo - git push heroku master 
- Open in browser with - heroku open 
Database
Using the Flask SQLAlchemy extension:
pip install flask-sqlalchemyand add this line to routes.py
app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://localhost/db_name'Create a models.py:
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
  __tablename__ = 'users'
  uid = db.Column(db.Integer, primary_key = True)
  firstname = db.Column(db.String(100))
  lastname = db.Column(db.String(100))
  email = db.Column(db.String(120), unique=True)
  pwdhash = db.Column(db.String(54))
  def __init__(self, firstname, lastname, email, password):
    self.firstname = firstname.title()
    self.lastname = lastname.title()
    self.email = email.lower()
    self.set_password(password)The main routes.py needs to initialize the db object:
brush:python
from models import db
db.init_app(app)Based on "Learning Flask" course at lynda.com, git repo
 
                
              