Skip to content

Commit a454058

Browse files
acsanymartin-martinbrendawelesbzaczynski
authored
Add materials for the FastAPI Jinja2 tutorial (#705)
* Add materials * Update code from review and fix requirements.txt file * Fix imports 🎉 * Adapt to tutorial code * Update README.md * Final QA --------- Co-authored-by: Martin Breuss <[email protected]> Co-authored-by: martin-martin <[email protected]> Co-authored-by: brendaweles <[email protected]> Co-authored-by: Bartosz Zaczyński <[email protected]>
1 parent 22c8cd6 commit a454058

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

fastapi-jinja2-template/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# How to Serve a Website With FastAPI Using HTML and Jinja2
2+
3+
This folder contains the code discussed in the tutorial [How to Serve a Website With FastAPI Using HTML and Jinja2](https://realpython.com/fastapi-jinja2-template/).
4+
5+
## Installation
6+
7+
The [recommended way to install FastAPI](https://realpython.com/get-started-with-fastapi/#install-fastapi-the-right-way) is with the `[standard]` extra dependencies. This ensures you get all the tools you need for developing an API without having to hunt down additional packages later:
8+
9+
```console
10+
$ python -m pip install "fastapi[standard]"
11+
```
12+
13+
The quotes around `"fastapi[standard]"` ensure the command works correctly across different [terminals](https://realpython.com/terminal-commands/) and operating systems. With the command above, you install several useful packages, including the [FastAPI CLI](https://fastapi.tiangolo.com/fastapi-cli/) and [`uvicorn`](https://www.uvicorn.org/), an [ASGI](https://en.wikipedia.org/wiki/Asynchronous_Server_Gateway_Interface) server for running your application.
14+
15+
You can also use the `requirements.txt` file in this folder and run `python -m pip install -r requirements.txt` to install the standard dependencies of FastAPI.

fastapi-jinja2-template/main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
from string import hexdigits
3+
4+
from fastapi import FastAPI, Request
5+
from fastapi.responses import HTMLResponse
6+
from fastapi.staticfiles import StaticFiles
7+
from fastapi.templating import Jinja2Templates
8+
9+
app = FastAPI()
10+
11+
app.mount("/static", StaticFiles(directory="static"), name="static")
12+
templates = Jinja2Templates(directory="templates")
13+
14+
15+
@app.get("/", response_class=HTMLResponse)
16+
def home(request: Request):
17+
hex_chars = "".join(random.choices(hexdigits.lower(), k=6))
18+
hex_color = f"#{hex_chars}"
19+
context = {
20+
"color": hex_color,
21+
}
22+
return templates.TemplateResponse(
23+
request=request, name="color.html", context=context
24+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fastapi[standard]==0.119.1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
document.querySelector('#copy-button').addEventListener('click', () => {
2+
const colorCode = document.querySelector('#color-code').textContent;
3+
navigator.clipboard.writeText(colorCode);
4+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
height: 100vh;
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
color: white;
7+
font-size: 120px;
8+
font-family: monospace;
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Random Color Generator</title>
6+
<link href="/static/style.css" rel="stylesheet">
7+
</head>
8+
<body>
9+
{% block content %}{% endblock content %}
10+
<script src="/static/script.js"></script>
11+
</body>
12+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<style>
5+
body {
6+
background-color: {{ color }};
7+
}
8+
</style>
9+
<div id="color-code">{{ color }}</div>
10+
<button id="copy-button">Copy Hex Code</button>
11+
{% endblock %}

0 commit comments

Comments
 (0)