comparison globalneighbors/web.py @ 6:316e1d54ffd4

move to jinja templates
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 15:47:59 -0700
parents 1b94f3bf97e5
children 254195d0bac2
comparison
equal deleted inserted replaced
5:7e27e874655b 6:316e1d54ffd4
5 """ 5 """
6 6
7 # imports 7 # imports
8 import argparse 8 import argparse
9 import json 9 import json
10 import os
10 import sys 11 import sys
11 import time 12 import time
12 from webob import Request, Response, exc 13 from webob import Request, Response, exc
13 from wsgiref import simple_server 14 from wsgiref import simple_server
14 from .locations import locations 15 from .locations import locations
15 from .locations import city_dict 16 from .locations import city_dict
16 from .read import read_cities 17 from .read import read_cities
17 from .read import read_city_list 18 from .read import read_city_list
18 from .schema import fields 19 from .schema import fields
19 from .schema import name 20 from .schema import name
21 from .template import template_dir
22 from .template import TemplateLoader
20 23
21 24
22 def autocomplete(cities, startswith=None): 25 def autocomplete(cities, startswith=None):
23 """autocomplete function for city names""" 26 """autocomplete function for city names"""
24 ### TODO: sort once, ahead of time 27 ### TODO: sort once, ahead of time
72 75
73 76
74 class GlobalHandler(Handler): 77 class GlobalHandler(Handler):
75 """WSGI HTTP Handler""" 78 """WSGI HTTP Handler"""
76 79
77 content_type = 'text/plain' 80 content_type = 'text/html'
78 81
79 def __init__(self, datafile): 82 def __init__(self, datafile, template_dir=template_dir):
80 self.datafile = datafile 83
81 84
82 # parse data 85 # parse data
86 self.datafile = datafile
83 self.cities = read_city_list(self.datafile, 87 self.cities = read_city_list(self.datafile,
84 fields=fields) 88 fields=fields)
85 self.locations = locations(self.cities) 89 self.locations = locations(self.cities)
86 90
87 # TODO: declare handlers 91 # declare handlers
88 self.handlers = {'/cities': 92 self.handlers = {'/cities':
89 CitiesHandler(self.locations, 93 CitiesHandler(self.locations,
90 self.cities)} 94 self.cities)}
95
96 # template loader
97 self.loader = TemplateLoader(template_dir)
98 self.index = self.loader.load('index.html')
91 99
92 100
93 def GET(self, request): 101 def GET(self, request):
94 if request.path_info in ('', '/'): 102 if request.path_info in ('', '/'):
95 # Landing page 103 # Landing page
96 return Response(content_type=self.content_type, 104 return Response(content_type=self.content_type,
97 body="""Global Neighbors 105 body=self.index.render(n_cities=len(self.cities)))
98 Serving {} cities""".format(len(self.cities)))
99 else: 106 else:
100 handler = self.handlers.get(request.path_info) 107 handler = self.handlers.get(request.path_info)
101 if handler: 108 if handler:
102 return request.get_response(handler) 109 return request.get_response(handler)
103 110
113 parser.add_argument('-p', '--port', dest='port', 120 parser.add_argument('-p', '--port', dest='port',
114 type=int, default=8080, 121 type=int, default=8080,
115 help="port to serve on") 122 help="port to serve on")
116 parser.add_argument('--hostname', dest='hostname', 123 parser.add_argument('--hostname', dest='hostname',
117 default='localhost', 124 default='localhost',
118 help="host name [DEFAULT: %(default)]") 125 help="host name [DEFAULT: %(default)s]")
119 options = parser.parse_args(args) 126 options = parser.parse_args(args)
120 127
121 # instantiate WSGI handler 128 # instantiate WSGI handler
122 app = GlobalHandler(datafile=options.cities) 129 app = GlobalHandler(datafile=options.cities)
123 130