home · contact · privacy
Improve explanation of missing data in HTML table view.
[berlin-corona-table] / enhance_table.py
index 02a7ac9b30b8cd0375a79a49468e2dc5c19e3d77..8d0fd335ec2c62e077bb3b34fcd47f10920adb30 100755 (executable)
@@ -17,6 +17,26 @@ district_pops = {
   'sum': 3754418,
 }
 
+# Map abbreviations to full names.
+translate = {
+  'CW': 'Charlottenburg-Wilmersdorf',
+  'FK': 'Friedrichshain-Kreuzberg',
+  'Li': 'Lichtenberg',
+  'MH': 'Marzahn-Hellersdorf',
+  'Mi': 'Mitte',
+  'Ne': 'Neukölln',
+  'Pa': 'Pankow',
+  'Re': 'Reinickendorf',
+  'Sp': 'Spandau',
+  'SZ': 'Steglitz-Zehlendorf',
+  'TS': 'Tempelhof-Schöneberg',
+  'TK': 'Treptow-Köpenick',
+  'sum': 'all of Berlin',
+  'wsum': 'sum for last 7 days',
+  'wavg': 'per-day average of new infections for last 7 days',
+  'winc': 'incidence (x per 100k inhabitants) of new infections for last 7 days',
+}
+
 # Read infections table path and output type.
 import sys
 if len(sys.argv) != 3:
@@ -111,23 +131,39 @@ for i in range(len(sorted_dates)):
 
 # Optimized for web browser viewing.
 if output_type == 'html':
-    print('<html>')
-    print('<style>')
-    print('table, tr, th, td { border: 1px solid black; }')
-    print('</style>')
-    print('<table>')
-    print('<tr>')
-    print('<th>date</th>')
+    print("""<!DOCTYPE html>
+<html>
+<head>
+<style>
+table, tr, th, td { border: 1px solid black; }
+.day_row:nth-child(7n+2) { background-color: yellow; }
+.district_name { writing-mode: vertical-rl; transform: rotate(180deg); }
+</style>
+<title>Table of Berlin's Corona infection number development by districts</title>
+</head>
+<h1>Table of Berlin's Corona infection number development by districts</h1>
+<p>Updated daily at 9pm. <a href="https://plomlompom.com/repos/?p=berlin-corona-table">Source code</a>. <a href="berlin_corona.txt">Text view optimized for terminal curl</a>.</p>
+<table>
+<tr>
+<th>date</th>""")
+    sorted_dates.reverse()
+    sum_district = sorted_districts[-1]
     for district in sorted_districts:
-        print('<th>%s</th>' % district)
+        long_form = translate[district]
+        if sum_district == district:
+            print('<th>%s</th>' % long_form)
+        else:
+            print('<th class="district_name">%s</th>' % long_form)
     print('</tr>')
-    sorted_dates.reverse()
     for date in sorted_dates:
-        print('<tr>')
+        print('<tr class="day_row">')
         print('<td>%s</td>' % date)
+        long_wsum = translate['wsum']
+        long_wavg = translate['wavg']
+        long_winc = translate['winc']
         for district in sorted_districts:
             district_data = db[district][date]
-            week_sum = week_avg = week_inc = ''
+            week_sum = week_avg = week_inc = '(not enough data)'
             new_infections = district_data['new_infections']
             if 'week_sum' in district_data:
                 week_sum = '%s' % district_data['week_sum']
@@ -136,12 +172,16 @@ if output_type == 'html':
             if 'week_incidence' in district_data:
                 week_inc = '%.1f' % district_data['week_incidence']
             print('<td>')
+            print(new_infections)
+            if district != sum_district:
+                print('<details><summary></summary>')
             print('<table>')
-            print('<tr><th>new</th><td>%s</td></tr>' % new_infections)
-            print('<tr><th>wsum</th><td>%s</td></tr>' % week_sum)
-            print('<tr><th>wavg</th><td>%s</td></tr>' % week_avg)
-            print('<tr><th>winc</th><td>%s</td></tr>' % week_inc)
+            print('<tr><th>%s</th><td>%s</td></tr>' % (long_wsum, week_sum))
+            print('<tr><th>%s</th><td>%s</td></tr>' % (long_wavg, week_avg))
+            print('<tr><th>%s</th><td>%s</td></tr>' % (long_winc, week_inc))
             print('</table>')
+            if district != sum_district:
+                print('</details>')
             print('</td>')
         print('</tr>')
     print('</table>')
@@ -153,28 +193,16 @@ elif output_type == 'txt':
     # Explain what this is.
     intro = \
 """Table of Berlin's Corona infection number development by districts.
-Updated daily around 9pm.
+Updated daily at 9pm.
 
 Abbrevations/explanations:
-
-CW: Charlottenburg-Wilmersdorf
-FK: Friedrichshain-Kreuzberg
-Li: Lichtenberg
-MH: Marzahn-Hellersdorf
-Mi: Mitte
-Ne: Neukölln
-Pa: Pankow
-Re: Reinickendorf
-Sp: Spandau
-SZ: Steglitz-Zehlendorf
-TS: Tempelhof-Schöneberg
-TK: Treptow-Köpenick
-sum: sum for all the districts
-wsum: sum for last 7 days
-wavg: per-day average of new infections for last 7 days
-winc: incidence (x per 100k inhabitants) of new infections for last 7 days
-
+"""
+    for k in translate:
+        intro += "%s: %s\n" % (k, translate[k])
+    intro += """
 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
+
+HTML view: https://plomlompom.com/berlin_corona.html
 """
     print(intro)