agenda/web_view.py

35 lines
835 B
Python
Raw Normal View History

2023-10-02 20:35:30 +01:00
#!/usr/bin/python3
"""Web page to show upcoming events."""
from datetime import date, datetime, timezone
from flask import Flask, render_template
from agenda import get_data
app = Flask(__name__)
app.debug = True
@app.route("/")
def index() -> str:
"""Index page."""
data = get_data()
now = datetime.now()
today = now.date()
now_utc = datetime.now(timezone.utc)
def days_hours(when: datetime) -> str:
delta = when - (now if when.tzinfo is None else now_utc)
return f"{delta.days:>5,d} days {delta.seconds // 3600:>2.0f} hours"
def days(when: date) -> str:
return " today" if when == today else f"{(when - today).days:>5,d} days"
return render_template("index.html", days=days, days_hours=days_hours, **data)
if __name__ == "__main__":
app.run(host="0.0.0.0")