Mercurial > hg > GlobalNeighbors
changeset 6:316e1d54ffd4
move to jinja templates
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 24 Jun 2017 15:47:59 -0700 |
parents | 7e27e874655b |
children | 254195d0bac2 |
files | globalneighbors/distance.py globalneighbors/template.py globalneighbors/templates/index.html globalneighbors/web.py setup.py tox.ini |
diffstat | 6 files changed, 64 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- 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): """
--- /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())
--- /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 @@ +<!DOCTYPE html> +<html> + <head> + <title>Global Neighbors</title> + </head> + <body> + <h1>Global Neighbors</h1> + <h2>Serving {{ n_cities }} cities</h2> + </body> +</html>
--- 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
--- 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 )