changeset 20:2fef925fbf37

display country + population in autocomplete drop down
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 16:12:08 -0700
parents 811adc9736eb
children 22c384fe954d
files globalneighbors/grid.py globalneighbors/neighbors.py globalneighbors/templates/index.html globalneighbors/web.py gunicorn.conf
diffstat 5 files changed, 44 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/globalneighbors/grid.py	Sun Jun 25 15:43:45 2017 -0700
+++ b/globalneighbors/grid.py	Sun Jun 25 16:12:08 2017 -0700
@@ -78,7 +78,8 @@
 class GriddedLocations(object):
 
     def __init__(self, locations):
-        raise NotImplementedError('TODO')
+        self.locations = locations
+        import pdb; pdb.set_trace()
 
 def main(args=sys.argv[1:]):
     """CLI"""
@@ -90,6 +91,9 @@
     # read locations
     city_locations = locations(parser.read_cities())
 
+    # make a grid
+    gridded_locations = GriddedLocations(locations)
+
 if __name__ == '__main__':
     main()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globalneighbors/neighbors.py	Sun Jun 25 16:12:08 2017 -0700
@@ -0,0 +1,26 @@
+"""
+read neighbors file;
+this should be in the form of:
+
+`{geoid: [(geoid_closest_neighbor, distance),
+          (geoid_2nd_closest_neighbor, distance),
+          ...]
+ }`
+"""
+
+import json
+import os
+
+string = (str, basestring)  # python2
+
+def read_neighbors_file(f):
+
+    if isinstance(f, string):
+        with open(f) as _f:
+            return read_neighbors_file(f)
+
+    retval = {}
+    for line in f:
+        data = json.loads(line)
+        retval.update(data)
+    return retval
--- a/globalneighbors/templates/index.html	Sun Jun 25 15:43:45 2017 -0700
+++ b/globalneighbors/templates/index.html	Sun Jun 25 16:12:08 2017 -0700
@@ -21,7 +21,7 @@
       template: {
         type: "custom",
         method: function (value, item) {
-          return '<a href="/' + item.geonameid + '">' + value + '</a>';
+          return '<a href="/' + item.geonameid + '"><b>' + value + '</b> ' + item["country code"] + '<br/><small>Population: ' + item.population + '</small></a>';
         }
       }
       };
--- a/globalneighbors/web.py	Sun Jun 25 15:43:45 2017 -0700
+++ b/globalneighbors/web.py	Sun Jun 25 16:12:08 2017 -0700
@@ -16,6 +16,7 @@
 from wsgiref import simple_server
 from .locations import locations
 from .locations import city_dict
+from .neighbors import read_neighbors_file
 from .read import read_cities
 from .read import read_city_list
 from .schema import fields
@@ -113,7 +114,10 @@
 
     content_type = 'text/html'
 
-    def __init__(self, datafile, template_dir=template_dir):
+    def __init__(self,
+                 datafile,
+                 neighbors_file=None,
+                 template_dir=template_dir):
 
 
         # parse data
@@ -121,6 +125,8 @@
         self.cities = read_city_list(self.datafile,
                                      fields=fields)
         self.locations = locations(self.cities)
+        if neighbors_file:
+            pass  # TODO
 
         # get country codes
         self.country_codes = sorted(set([city['country code']
@@ -180,10 +186,13 @@
     parser.add_argument('--hostname', dest='hostname',
                         default='localhost',
                         help="host name [DEFAULT: %(default)s]")
+    parser.add_argument('--neighbors', dest='neighbors_file',
+                        help="file for nearest neighbors")
     options = parser.parse_args(args)
 
     # instantiate WSGI handler
-    app = GlobalHandler(datafile=options.cities)
+    app = GlobalHandler(datafile=options.cities,
+                        neighbors_file=options.neighbors_file)
 
     # wrap it in a static file server
     app = PassthroughFileserver(app)
--- a/gunicorn.conf	Sun Jun 25 15:43:45 2017 -0700
+++ b/gunicorn.conf	Sun Jun 25 16:12:08 2017 -0700
@@ -1,2 +1,2 @@
 [program:globalneighbors]
-command=/home/jhammel/GlobalNeighbors/bin/gunicorn -w 2 --chdir /home/jhammel/GlobalNeighbors/src/GlobalNeighbors  -b 0.0.0.0:8080 wsgiapp:application 
\ No newline at end of file
+command=/home/jhammel/GlobalNeighbors/bin/gunicorn -w 2 --chdir /home/jhammel/GlobalNeighbors/src/GlobalNeighbors  -b 0.0.0.0:8080 wsgiapp:application