changeset 23:6891c5523b69

load with neighbors :)
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 18:13:43 -0700
parents e69cb496324e
children 40a9850076a4
files README.txt globalneighbors/neighbors.py globalneighbors/templates/city.html globalneighbors/web.py wsgiapp.py
diffstat 5 files changed, 44 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/README.txt	Sun Jun 25 17:45:19 2017 -0700
+++ b/README.txt	Sun Jun 25 18:13:43 2017 -0700
@@ -73,6 +73,13 @@
 There are 72897 cases with less than 100 in this box and
 18444 with less than 10.
 
+Because of this, the closest 50 neighbors were
+precomputed and cached as `neighbors.dat`:
+
+```
+find-neighbors /home/jhammel/tensorflow/cities1000.txt -k 50 --latlon 1. 1. neighbors.dat
+```
+
 
 ## Web Service
 
@@ -101,6 +108,8 @@
   it is a `O(10^10)` operation on the dataset.  This should be improved
   and parallelized
 
+- no through-the-web (TTW) testing was done except manual.
+  This should be corrected with Selenium and other headless testing
 
 ## (Hopefully) Helpful Links
 
--- a/globalneighbors/neighbors.py	Sun Jun 25 17:45:19 2017 -0700
+++ b/globalneighbors/neighbors.py	Sun Jun 25 18:13:43 2017 -0700
@@ -22,7 +22,7 @@
 
     if isinstance(f, string):
         with open(f) as _f:
-            return read_neighbors_file(f)
+            return read_neighbors_file(_f)
 
     retval = {}
     for line in f:
--- a/globalneighbors/templates/city.html	Sun Jun 25 17:45:19 2017 -0700
+++ b/globalneighbors/templates/city.html	Sun Jun 25 18:13:43 2017 -0700
@@ -24,5 +24,22 @@
       <dt>Country:</dt>
       <dd>{{ city['country code'] }}
     </dl>
+
+    {% if neighbors %}
+    <div>
+    <p>
+      {{ neighbors|length }} nearest neighbors:
+    </p>
+    <table>
+      <tr><th>Neighbor</th><th>Distance (km)</th></tr>
+      {% for neighbor in neighbors %}
+      <tr>
+        <td><a href="/{{ neighbor.geoid }}">{{ neighbor.name }}</a></td>
+        <td>{{ neighbor.distance }}</td>
+      </tr>
+      {% endfor %}
+    </table>
+    </div>
+    {% endif %}
   </body>
 </html>
--- a/globalneighbors/web.py	Sun Jun 25 17:45:19 2017 -0700
+++ b/globalneighbors/web.py	Sun Jun 25 18:13:43 2017 -0700
@@ -180,9 +180,20 @@
                 city = self.cities.get(geoid)
                 if not city:
                     return
-                variables = dict(city=city)
+                variables = dict(city=city, neighbors=None)
                 if self.neighbors:
-                    import pdb; pdb.set_trace()
+                    n_neighbors = request.GET.get('neighbors', 10)
+                    try:
+                        n_neighbors = int(n_neighbors)
+                    except ValueError as e:
+                        n_neighbors = 10
+                    neighbors = self.neighbors.get(geoid, [])[:n_neighbors]
+                    neighbors = [{'name': self.cities[geoid]['name'],
+                                  'geoid': geoid,
+                                  'distance': distance}
+                                 for geoid, distance in neighbors]
+                    variables['neighbors'] = neighbors
+
                 return Response(content_type=self.content_type,
                                 body=self.citypage.render(variables))
             except ValueError:
--- a/wsgiapp.py	Sun Jun 25 17:45:19 2017 -0700
+++ b/wsgiapp.py	Sun Jun 25 18:13:43 2017 -0700
@@ -15,8 +15,11 @@
     # XXX use a default not really appropriate for production
     cities1000 = os.path.join(here, 'tests', 'data', 'cities1000.txt')
 assert os.path.exists(cities1000)
+neighbors = '/home/jhammel/GlobalNeighbors/neighbors.dat'
+assert os.path.exists(neighbors)
 
 # WSGI App
-application = PassthroughFileserver(GlobalHandler(cities1000))
+application = PassthroughFileserver(GlobalHandler(cities1000,
+                                                  neighbors_file=neighbors))
 print ("Finished loading application")
 sys.stdout.flush()