This line of code defines a FastAPI route that serves your homepage (index.html) — the User Interface (UI) of your web app.
Let’s go step-by-step, clearly and practically 👇
@app.get("/", response_class=HTMLResponse)
async def serve_ui(request: Request):
log.info("Serving UI homepage.")
resp = templates.TemplateResponse("index.html", {"request": request})
resp.headers["Cache-Control"] = "no-store"
return resp
@app.get("/")This is a decorator that defines a GET endpoint at the root URL (/).
💡 So when you visit:
http://localhost:8000/
FastAPI will call this function:
serve_ui()
It’s basically your home page route.
response_class=HTMLResponseThis tells FastAPI that this route will return an HTML webpage, not JSON.
✅ Normally FastAPI returns JSON (for APIs),
but here we’re rendering a web page (like index.html), so we explicitly tell it to expect HTML.
async def serve_ui(request: Request):This defines the function that runs when the user opens the home page.
async → means this function can run asynchronously (non-blocking I/O).
request: Request → gives access to the user’s request data (IP, headers, etc.), required by Jinja2 templates.
log.info("Serving UI homepage.")This logs a message to your server logs.
✅ It’s useful for debugging — every time someone visits /, you’ll see:
INFO: Serving UI homepage.
templates.TemplateResponse("index.html", {"request": request})This is the heart of the code 💖
templates → an instance of Jinja2Templates(directory="templates")
"index.html" → your HTML file inside the templates folder
{"request": request} → sends the request object to the HTML file, required by Jinja2 for rendering URLs and static assets.
So it renders:
templates/index.html
and returns it as the page content.
resp.headers["Cache-Control"] = "no-store"This line adds a header to the response that prevents browsers from caching the page.
✅ Meaning:
Every time someone opens /, the browser will fetch a fresh copy instead of showing an old cached version.
This is especially important during development or for dynamic dashboards.
return respFinally, it sends the rendered HTML page (with the “no-store” header) as a response to the browser.
GET http://localhost:8000/
Matches / route
Logs “Serving UI homepage.”
Loads templates/index.html
Wraps it in an HTMLResponse
Adds “Cache-Control: no-store”
Sends HTML page to browser
| Component | Purpose |
|---|---|
@app.get("/") |
Defines the homepage route |
response_class=HTMLResponse |
Tells FastAPI to return HTML |
request: Request |
Provides request data to templates |
TemplateResponse("index.html", {...}) |
Renders the homepage |
Cache-Control: no-store |
Prevents caching |
return resp |
Sends the page back to browser |
project/
│
├── main.py
├── templates/
│ └── index.html
└── static/
├── style.css
└── logo.png
When you open:
http://localhost:8000/
You’ll see your homepage UI, styled using your /static files.
If you make a change to index.html, you’ll see it immediately —
because of this header 👇
resp.headers["Cache-Control"] = "no-store"