Nov 6
aside
(Not just the product itself, but the display of what it is. Very nice.)
Homework
Go over homework, and your PHP code.
Any last discussion on PHP stuff?
Final project
Your main task for the rest of the semester - about a month -
is to implement a web app using a modern framework.
We'll be looking at Flask (in python), which is what I suggest
unless you have a strong preference otherwise. It should
have a database, multiple related tables, sessions,
and enough web pages to see and edit these things.
Tentative final project schedule:
- proposal next week, Nov 13
- progress report Nov 20 (Thanksgiving week)
- rough draft due Nov 27 ; I will give feedback
- class presentation Dec 4 (last class)
- final version due by Fri Dec 7
Web Frameworks
We now the major components that go into web programming :
- HTTP protocol
- HTML, CSS, JavaScript - client side
- cookies, headers, CGI - server side
- SQL relational database
- PHP - old school
... and we're ready to pull all these technologies
(and a few more) into the big picture : the
currently in vogue "Web Framework" model.
Read
Companion techologies
- local programming environment & packages (e.g. virtualenv)
- revision control (e.g. git, mercuruial, subversion)
- development / testing, staging, deployment strategies
- CSS & JavaScript compilers (e.g. SASS, CoffeeScript)
Question:
- Do we want to focus on Flask? (A faily clean python "microframework")
- Or do many different things? (Django, Rails, CakePHP, ...)
Sam's Flask logging example
Flask 101
Well, now that you've seen "Hello world",
let's start looking some components.
One piece of syntax that Flask uses
that we haven't talked yet is called a "decorator",
which look like this:
@memoize
def fib(x):
if x <= 1:
return 1
else:
return fib(x-1) + fib(x-2)
The "@something" syntax modifies the function that
immediately follows. Typically when you use this notation
you'll be using someone else's decorators, though it
is possible to define your own. Flask uses this notation
quite a lot, to set roles for functions that you define.
We'll see examples as we go along.
What this notation actually does is
def fib(x):
if x <= 1:
return 1
else:
return fib(x-1) + fib(x-2)
fib = memoize(fib)
where memoize needs to have been defined somewhere else.
For example,
def memoize(func):
closure = {}
def f(*args):
if closure.has_key(args):
return closure[args]
else:
result = func(*args)
closure[args] = result
return result
return f
What do you think that does?
Second, there is also some talk of python "packages"
in the Flask docs. This is a set of namespace file & folder
conventions :
http://guide.python-distribute.org/creation.html
Basically, small python applications fit well in a few files
which "import foo" each other. Bigger things (and this sometimes
describes web apps) with sub-folders fit into a different
model, where "import project.database" seems more appropriate.
Third, be aware that utf-8 characters
aren't allowed in .py files by default.
If you want 'em, put this line near the top:
# -*- coding: utf-8 -*-
Next is the idea of a template. By default
Flask uses an engine called Jinja2
from jinja2 import *
template = Template("Hello {{name}}. How are you?")
result = template.render(name = 'John')
# same : template.render({'name':'John'})
print result
t2 = Template("""
{% for i in (1, 12, 23)%}
The square of {{i}} is {{i**2}}.
{% endfor %}""")
print t2.render()
In the context of Flask, there are also various
environments it is aware of (request, response, globals)
types of templates (.html, .xml), and some conventions
about what gets "escaped" when.
Next there is the idea of "routes",
namely a way to say which URLs trigger
which responses.
Then there is the model of running a Flask app:
one way during development, and something else
when it is "deployed".
Tests are also typically a part of the story,
as well as a "console" environment for interactively
triggering or calling or adjusting things.