Build a Countdown Dashboard with FastAPI and Jinja2 – Your Personal “Moyu Office”

This tutorial demonstrates building a FastAPI‑powered “Moyu Office” page that dynamically shows today’s date, weekday, and the remaining days until the weekend and major Chinese holidays, using Jinja2 templates, the zhdate library for lunar‑solar conversion, and deployment behind Nginx.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Countdown Dashboard with FastAPI and Jinja2 – Your Personal “Moyu Office”

Introduction

In this tutorial I show how to create a simple “Moyu Office” countdown page that displays the current date, weekday and the number of days remaining until various holidays and the weekend.

Implementation Overview

Use FastAPI as the backend framework and Jinja2Templates for dynamic HTML rendering.

Use the zhdate library to convert between Gregorian and lunar calendars for holidays such as Chinese New Year, Dragon Boat Festival and Mid‑Autumn Festival.

Calculate the distance in days for each holiday, handling past dates by adding one year.

Define a helper function get_week_day to map Python weekday numbers to Chinese weekday names.

Collect all distances into a list of dictionaries and sort them by the remaining days.

Expose a single route “/” that renders readme.html with the computed data.

Deploy the application behind Nginx.

Key Code Snippets

# -*- coding: utf-8 -*-
import datetime
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from zhdate import ZhDate as lunar_date

app = FastAPI(debug=False, title="My API")
templates = Jinja2Templates(directory="templates")
today = datetime.date.today()
# calculate distances ...

def get_week_day(date):
    week_day_dict = {0:"星期一",1:"星期二",2:"星期三",3:"星期四",4:"星期五",5:"星期六",6:"星期天"}
    return week_day_dict[date.weekday()]

@app.get("/", response_class=HTMLResponse)
async def readme(request: Request):
    return templates.TemplateResponse("readme.html", {"request": request, "time_": time_, "now_": now_, "week_day_": week_day_})

The corresponding Jinja2 template loops over time_ and prints each holiday with its remaining days.

{% for v_ in time_ %}
<p>🐟 距离 {{ v_.title }} 放假还有 {{ v_.v_ }} 天</p>
{% else %}
<p>沒有任何值</p>
{% endfor %}

The project source code is hosted at https://github.com/PY-GZKY/moyu.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonWeb DevelopmentNginxFastAPIJinja2HolidayCountdown
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.