#!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- from flask import Flask, render_template, url_for, request import math import random app = Flask(__name__) @app.route('/') def welcome(): exes = [ 'capitals/Paris', 'sqrt/16.0', 'sqrt/17.1', 'print_stmt', 'console_error', 'no_view', 'urls', 'canonical', 'canonical/', 'fragile', 'fragile/', 'render/', 'hello/', 'tim/', 'things/', 'reading/', 'render_styled/', 'form/', ] links = [ '
  • {url}
  • '.format(url=ex) for ex in exes ] return ('''

    Welcome

    Hello World! These are the examples for lecture 2.

    Try

    ''' .format(examples=''.join(links))) @app.route('/capitals/') def capitals(city): known = {'Paris':'France', 'London':'England', 'Madrid':'Spain', 'Beijing':'China'} if city in known: return ('

    {city} is the capital of {country}' .format(city=city,country=known[city])) else: return ('''

    I don't know the country whose capital is {city}''' .format(city=city)) @app.route('/sqrt/') def sqrt(n): return ('

    {x} is the square root of {y}' .format(x=math.sqrt(abs(n)),y=n)) @app.route('/print_stmt/') def print_stmt(): n = random.randint(1,10) print 'n is ',n return '

    Can you guess your lucky number?' @app.route('/console_error/') def console_error(): # this doesn't work roman = ['x','v','i','c','m','d','l'] s = random.choice(roman) n = int(s) print ('{roman} converts to {arabic}' .format(roman=s,arabic=n)) @app.route('/no_view/') def no_view(): n = random.randint(1,10) if n < 9: print '

    Keep trying, you only scored a {n}'.format(n=n) else: return '

    Wow, you scored a 10!!' @app.route('/urls/') def urls(): hrefs = [ url_for('print_stmt'), url_for('capitals', city='London'), url_for('sqrt', n=18) ] links = [ '

    {h}'.format(h=h) for h in hrefs ] return ''.join(links) @app.route('/canonical/') def canonical(): return '

    Well done!' @app.route('/fragile') def fragile(): return '

    Maybe?' @app.route('/hello/') def hello(): nom = random.choice(['Inigo Montoya','Harry','Ron','Hermione']) return render_template('name.html', name = nom) @app.route('/tim/') def tim(): art = {'name': 'Sir Galahad of Camelot', 'quest': 'To seek the grail', 'color': 'blue, no, I mean YEELLLLLLLLLOOOOOOOWWWWWWW'} return render_template('tim.html', dic = art) @app.route('/things/') def things(): favs = ['Raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens', 'brown paper packages tied up with strings'] return render_template('things.html', things = favs) @app.route('/reading/') def reading(): stuff = {'title' : '''Material for class on St. Patrick's Day''', 'classdate' : '3/17/2017', 'article' : u'céad míle fáilte or a hundred thousand welcomes'} return render_template('reading.html', **stuff) @app.route('/render/') def render(): return render_template('trio.html',a='Harry',b='Ron',c='Hermione') @app.route('/render_styled/') def render_styled(): return render_template('trio-styled.html',a='Harry',b='Ron',c='Hermione') @app.route('/handle_form/', methods= ['GET','POST']) def handle_form(): if request.method == 'POST': for k in request.form: print k, request.form[k] return render_template('form_results.html',quest=request.form['quest']) @app.route('/form/') def form(): return render_template('form.html') if __name__ == '__main__': app.debug = True app.run('0.0.0.0',2716)