agenda/web_view.py
2023-10-05 11:07:01 +01:00

35 lines
828 B
Python
Executable file

#!/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:,d} days"
return render_template("index.html", days=days, days_hours=days_hours, **data)
if __name__ == "__main__":
app.run(host="0.0.0.0")