changeset 26:7def5611895b

Add support for an index page displaying all locations. This seems as good a time as any to declare 0.1.0.
author Paul Fisher <paul@pfish.zone>
date Sun, 10 Nov 2019 23:46:51 -0500
parents a4147ecb18b3
children 99b0759386b1
files setup.py weather_server/locations.py weather_server/server.py weather_server/static/style.css weather_server/templates/index.html weather_server/templates/location.html
diffstat 6 files changed, 111 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Sun Nov 10 23:43:37 2019 -0500
+++ b/setup.py	Sun Nov 10 23:46:51 2019 -0500
@@ -2,7 +2,7 @@
 
 setuptools.setup(
     name='weather-server',
-    version='0.0.8',
+    version='0.1.0',
     packages=setuptools.find_packages(),
     python_requires='>=3.7',
     install_requires=[
--- a/weather_server/locations.py	Sun Nov 10 23:43:37 2019 -0500
+++ b/weather_server/locations.py	Sun Nov 10 23:46:51 2019 -0500
@@ -20,9 +20,10 @@
 
 class Location:
 
-    def __init__(self, root: pathlib.Path):
+    def __init__(self, root: pathlib.Path, key: str):
         parser = configparser.ConfigParser(interpolation=None)
         self.root = root
+        self.key = key
         config_file = root / CONFIG_FILE
         try:
             with open(config_file, 'r') as infile:
@@ -81,6 +82,9 @@
         locs, _ = self.info
         return locs[name]
 
+    def locations(self) -> t.Dict[str, Location]:
+        return self.info[0]
+
     def _maybe_reload(self) -> None:
         new_mtime = self.root.stat().st_mtime_ns
         _, old_mtime = self.info
@@ -89,7 +93,7 @@
         locations = {}
         for child in self.root.iterdir():
             try:
-                locations[child.name] = Location(child)
+                locations[child.name] = Location(child, child.name)
             except ConfigError:
                 pass  # It's OK. Skip this.
         self.info = locations, new_mtime
--- a/weather_server/server.py	Sun Nov 10 23:43:37 2019 -0500
+++ b/weather_server/server.py	Sun Nov 10 23:46:51 2019 -0500
@@ -22,7 +22,11 @@
 
     @app.route('/')
     def home():
-        return 'Weather server'
+        locations = tuple(locs.locations().values())
+        return flask.render_template(
+            'index.html',
+            locations=locations,
+        )
 
     @app.route('/_submit', methods=['POST'])
     def submit():
--- a/weather_server/static/style.css	Sun Nov 10 23:43:37 2019 -0500
+++ b/weather_server/static/style.css	Sun Nov 10 23:46:51 2019 -0500
@@ -5,6 +5,11 @@
     width: 100%;
 
     font-family: Roboto, sans-serif;
+    background: var(--root-background);
+    color: var(--root-color);
+
+    --root-background: #263238;
+    --root-color: white;
 
     --temp-background: #0d47a1;
     --temp-text: white;
@@ -15,6 +20,9 @@
 body {
     margin: 0;
     padding: 0;
+}
+
+body.location {
     display: flex;
     flex-flow: column nowrap;
     width: 100%;
@@ -28,8 +36,8 @@
     font: inherit;
 
     flex: none;
-    background: #263238;
-    color: white;
+    background: var(--root-background);
+    color: var(--root-color);
     padding: 12px 24px;
     font-size: 24px;
 
@@ -38,11 +46,15 @@
     justify-content: center;
 }
 
+h1 a {
+    color: inherit;
+}
+
 h1 span {
     display: block;
 }
 
-p {
+.location p {
     margin: 0;
     padding: 0;
     font: inherit;
@@ -50,7 +62,7 @@
     line-height: 1;
 }
 
-.plain p {
+.location.plain p {
     box-sizing: border-box;
     flex: 1;
     position: relative;
@@ -60,33 +72,33 @@
     padding: 24px;
 }
 
-.plain p.important .key {
+.location.plain p.important .key {
     display: block;
     opacity: 75%;
     font-size: 18px;
 }
 
-.plain p.important .value {
+.location.plain p.important .value {
     margin-top: 0;
     font-size: 150px;
     display: flex;
     flex-flow: row nowrap;
 }
 
-.plain p.important .value .unit {
+.location.plain p.important .value .unit {
     font-size: 33%;
     font-weight: bold;
     opacity: 66%;
     padding-top: 0.25em;
 }
 
-.fancy p.important {
+.location.fancy p.important {
     position: relative;
     flex: 1;
     height: 200px;
 }
 
-.fancy p.important canvas {
+.location.fancy p.important canvas {
     display: block;
     position: absolute;
     top: 0;
@@ -97,7 +109,7 @@
     height: 100%;
 }
 
-.fancy p.important .key {
+.location.fancy p.important .key {
     display: block;
     position: absolute;
     top: 24px;
@@ -110,21 +122,21 @@
     opacity: 75%;
 }
 
-.fancy p.important .value {
+.location.fancy p.important .value {
     display: none;
 }
 
-#temp {
+.location #temp {
     background: var(--temp-background);
     color: var(--temp-text);
 }
 
-#dewpoint {
+.location #dewpoint {
     background: var(--dewpoint-background);
     color: var(--dewpoint-text);
 }
 
-#timestamp {
+.location #timestamp {
     flex: none;
     background: #263238;
     color: white;
@@ -136,6 +148,43 @@
     justify-content: center;
 }
 
-#timestamp > span {
+.location #timestamp > span {
     display: block;
 }
+
+.home a {
+    color: inherit;
+}
+
+.home ul, .home li {
+    list-style: none;
+    margin: 0;
+    padding: 0;
+}
+
+.home li:nth-child(odd) {
+    background: var(--temp-background);
+    color: var(--temp-text);
+}
+
+.home li:nth-child(even) {
+    background: var(--dewpoint-background);
+    color: var(--dewpoint-text);
+}
+
+.home li a {
+    display: block;
+    justify-content: space-between;
+    padding: 15px 24px;
+    font-size: 18px;
+    display: flex;
+    flex-flow: row wrap;
+}
+
+.home li a b {
+    display: block;
+}
+
+.home li a span {
+    display: block;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/weather_server/templates/index.html	Sun Nov 10 23:46:51 2019 -0500
@@ -0,0 +1,33 @@
+<!doctype html>
+<html>
+    <head>
+        <title>The Weather In Places</title>
+        <link
+            rel="stylesheet"
+            href="{{ url_for('static', filename='style.css') }}">
+        <link
+            rel="shortcut icon"
+            href="{{ url_for('static', filename='thermometer.png') }}">
+    </head>
+    <body class="home">
+        <h1><span>Weather</span></h1>
+        <ul>
+            {% for location in locations %}
+            <li>
+                <a href="{{ url_for('show', location=location.key) }}">
+                    <b>{{ location.name }}</b>
+                    <span>
+                        {%- set reading = location.latest() -%}
+                        {%- if reading -%}
+                            {{ reading.temp_f|round|int -}}
+                            &nbsp;°F
+                        {%- else -%}
+                            ???
+                        {%- endif -%}
+                    </span>
+                </a>
+            </li>
+            {% endfor %}
+        </ul>
+    </body>
+</html>
--- a/weather_server/templates/location.html	Sun Nov 10 23:43:37 2019 -0500
+++ b/weather_server/templates/location.html	Sun Nov 10 23:46:51 2019 -0500
@@ -10,8 +10,8 @@
             rel="shortcut icon"
             href="{{ url_for('static', filename='thermometer.png') }}">
     </head>
-    <body class="plain">
-        <h1><span>{{ location.name }} conditions</span></h1>
+    <body class="location plain">
+        <h1><span><a href="{{ url_for('home') }}">Weather</a>: {{ location.name }}</span></h1>
         {% if last_reading %}
             <p class="important" id="temp">
                 <span class="key">Temperature</span>