~humaid/areweherdimmuneyet

98bc209cc9b0d8532c5abf6286bae0f2a541b70e — Humaid AlQassimi 8 months ago acd7c3c
Render HTML with templates and add date countdown
3 files changed, 32 insertions(+), 5 deletions(-)

M .gitignore
M main.py
R index.html => template.html
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
public/
chart.svg
index.html

M main.py => main.py +18 -1
@@ 3,8 3,9 @@ import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import sys
from datetime import date, timedelta
from datetime import date, timedelta, datetime
import requests
from quik import FileLoader

# Doses goal for calculations
DOSES_GOAL = 140


@@ 21,8 22,23 @@ def get_herd_immunity():
    x0 = (model - DOSES_GOAL).roots
    return (model, mdates.num2date(x0[0]))

def gen_html(estimate):
    print("Rendering HTML")
    loader = FileLoader('.')
    template = loader.load_template('template.html')
    estimate_txt = estimate.strftime("%d %B")
    res = template.render({'estimate': estimate_txt,
        'goalMs': estimate.timestamp(),
        'today': date.today().strftime("%d %B"),
        'ver': round(datetime.now().timestamp())},
                  loader=loader).encode('utf-8')
    f = open("index.html", "w")
    f.write(res.decode("utf-8"))
    f.close()


def gen_img():
    print("Generating image")
    model, estimate = get_herd_immunity()
    estimate_txt = estimate.strftime("%d %b")



@@ 65,6 81,7 @@ def gen_img():
    plt.savefig('chart.svg')

    print("We will reach herd immunity at: " + estimate_txt)
    gen_html(estimate)


def pull_data():

R index.html => template.html +13 -4
@@ 45,8 45,10 @@ img {
  <h2 class="answer">No</h2>
  <p>At the current rate, we estimate that we will reach herd
  immunity by</p>
  <p class="date">30<sup>th</sup> March</p>
  <img src="chart.svg?v=26" />
  <p class="date">@estimate</p>
  <p id="est"><noscript><small>Enable JavaScript to show estimate
      countdown! Code is released under BSD-2.</small></noscript></p>
  <img src="chart.svg?v=@ver" />
  <p><small>We have chosen 140 doses per 100 people (~70%) as the goal for herd
    immunity, which is not based on scientific research. This estimate is
    predicted using polynomial regression, expect it to change frequently.


@@ 54,6 56,13 @@ img {
        goal</b>.<br>
  This is a hobbyist website, which is unaffiliated with any authority or
  official body.</small></p>
  <p><small>Last updated: 12 February</small></p>
  <p><small>Last updated: @today</small></p>
  <p><small><a href="https://git.sr.ht/~humaid/areweherdimmuneyet"
    target="src">Source</a></small></p>
               target="src">Source</a></small></p>
  <script>
const date1 = @goalMs;
const ONE = 1000 * 60 * 60 * 24; // millis * seconds * minutes * hours
const diff = Math.abs(date1 - (new Date()).getMilliseconds());
est = Math.floor(diff/ONE);
document.getElementById("est").innerHTML = "(in "+est+" days)";
  </script>