Mercurial > hg > GlobalNeighbors
diff 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 |
line wrap: on
line diff
--- a/globalneighbors/web.py Sat Jun 24 15:16:10 2017 -0700 +++ b/globalneighbors/web.py Sat Jun 24 15:47:59 2017 -0700 @@ -7,6 +7,7 @@ # imports import argparse import json +import os import sys import time from webob import Request, Response, exc @@ -17,6 +18,8 @@ from .read import read_city_list from .schema import fields from .schema import name +from .template import template_dir +from .template import TemplateLoader def autocomplete(cities, startswith=None): @@ -74,28 +77,32 @@ class GlobalHandler(Handler): """WSGI HTTP Handler""" - content_type = 'text/plain' + content_type = 'text/html' - def __init__(self, datafile): - self.datafile = datafile + def __init__(self, datafile, template_dir=template_dir): + # parse data + self.datafile = datafile self.cities = read_city_list(self.datafile, fields=fields) self.locations = locations(self.cities) - # TODO: declare handlers + # declare handlers self.handlers = {'/cities': CitiesHandler(self.locations, self.cities)} + # template loader + self.loader = TemplateLoader(template_dir) + self.index = self.loader.load('index.html') + def GET(self, request): if request.path_info in ('', '/'): # Landing page return Response(content_type=self.content_type, - body="""Global Neighbors - Serving {} cities""".format(len(self.cities))) + body=self.index.render(n_cities=len(self.cities))) else: handler = self.handlers.get(request.path_info) if handler: @@ -115,7 +122,7 @@ help="port to serve on") parser.add_argument('--hostname', dest='hostname', default='localhost', - help="host name [DEFAULT: %(default)]") + help="host name [DEFAULT: %(default)s]") options = parser.parse_args(args) # instantiate WSGI handler