Web
Programming

Fall 2012
course
navigation

Nov 6

aside

Cool website of the day: http://maker.github.com/ratchet/
(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:

Web Frameworks

We now the major components that go into web programming :
... 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
Question:

Sam's Flask logging example

I've made a simple logging example which can be viewed here: http://csmarlboro.org:5000/
The source can be seen here: http://csmarlboro.org/sam/flask/

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.
See http://guide.python-distribute.org/creation.html

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.

The docs are here : http://flask.pocoo.org/docs/ ; your next job is to explore these, then come back with questions.
http://cs.marlboro.edu/ courses/ fall2012/web/ notes/ Nov_6
last modified Tuesday November 6 2012 4:43 pm EST