# HG changeset patch # User Jeff Hammel # Date 1498344479 25200 # Node ID 316e1d54ffd44a4048e55e7aeb653f69aef8b9ad # Parent 7e27e874655beaeb6c489d778844cc660f2051c5 move to jinja templates diff -r 7e27e874655b -r 316e1d54ffd4 globalneighbors/distance.py --- a/globalneighbors/distance.py Sat Jun 24 15:16:10 2017 -0700 +++ b/globalneighbors/distance.py Sat Jun 24 15:47:59 2017 -0700 @@ -48,6 +48,15 @@ else: distances.append((i, new_distance)) +# def insert_distance(distances, i, new_distance, k): +# if not distances: +# distances.append((i, new_distance)) +# return +# if new_distance >= distances[-1][-1]: +# if len(distances) < k: +# distances.append((i, new_distance)) +# return + def calculate_distances(locations, r=Rearth): """ diff -r 7e27e874655b -r 316e1d54ffd4 globalneighbors/template.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globalneighbors/template.py Sat Jun 24 15:47:59 2017 -0700 @@ -0,0 +1,23 @@ +""" +functionality for `jinja` templates +""" + +import os +from jinja2 import Template + +# default template location +here = os.path.dirname(os.path.abspath(__file__)) +template_dir = os.path.join(here, 'templates') + + +class TemplateLoader(object): + + def __init__(self, template_dir=template_dir): + assert os.path.exists(template_dir) + self.template_dir = template_dir + + def load(self, template): + path = os.path.join(self.template_dir, template) + assert os.path.exists(path) + with open(path) as f: + return Template(f.read()) diff -r 7e27e874655b -r 316e1d54ffd4 globalneighbors/templates/index.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globalneighbors/templates/index.html Sat Jun 24 15:47:59 2017 -0700 @@ -0,0 +1,10 @@ + + + + Global Neighbors + + +

Global Neighbors

+

Serving {{ n_cities }} cities

+ + diff -r 7e27e874655b -r 316e1d54ffd4 globalneighbors/web.py --- 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 diff -r 7e27e874655b -r 316e1d54ffd4 setup.py --- a/setup.py Sat Jun 24 15:16:10 2017 -0700 +++ b/setup.py Sat Jun 24 15:47:59 2017 -0700 @@ -5,7 +5,10 @@ import os version = "0.0" -dependencies = ['webob', 'gunicorn'] +dependencies = ['gunicorn', + 'jinja2', + 'webob'] + # allow use of setuptools/distribute or distutils kw = {} @@ -37,11 +40,11 @@ classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers author='Jeff Hammel', author_email='k0scist@gmail.com', - url='', + url='http://k0s.org/hg/GlobalNeighbors', license='', packages=['globalneighbors'], include_package_data=True, - tests_require=['tox'], + tests_require=['tox', 'pytest'], zip_safe=False, **kw ) diff -r 7e27e874655b -r 316e1d54ffd4 tox.ini --- a/tox.ini Sat Jun 24 15:16:10 2017 -0700 +++ b/tox.ini Sat Jun 24 15:47:59 2017 -0700 @@ -1,7 +1,9 @@ # https://tox.readthedocs.io/en/latest/ [tox] envlist=py27 + [testenv] deps = pytest + jinja2 commands=pytest tests/