Skip to content

Commit e684693

Browse files
committed
Add materials
1 parent ddcd360 commit e684693

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-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 repository contains the code discussed in the associated tutorial [Get Started With FastAPI](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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
def generate_color():
16+
return f"#{''.join(random.choices(hexdigits.lower(), k=6))}"
17+
18+
19+
@app.get("/", response_class=HTMLResponse)
20+
def home():
21+
html = """
22+
<!DOCTYPE html>
23+
<html lang="en">
24+
<head>
25+
<meta charset="UTF-8">
26+
<title>Home</title>
27+
</head>
28+
<body>
29+
<h1>Welcome to FastAPI!</h1>
30+
</body>
31+
</html>
32+
"""
33+
return html
34+
35+
36+
@app.get("/random-color", response_class=HTMLResponse)
37+
def random_color(request: Request):
38+
color = generate_color()
39+
return templates.TemplateResponse(
40+
request=request, name="color.html", context={"color": color}
41+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fastapi==0.118.0
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', function() {
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>{% block title %}Color Palette Generator{% endblock %}</title>
6+
<link href="/static/style.css" rel="stylesheet">
7+
</head>
8+
<body>
9+
<main>
10+
{% block content %}{% endblock content %}
11+
</main>
12+
<script src="/static/script.js"></script>
13+
</body>
14+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}Random Color: {{ color }}{% endblock %}
4+
5+
{% block content %}
6+
<style>
7+
body {
8+
background-color: {{ color }};
9+
}
10+
</style>
11+
<div id="color-code">{{ color }}</div>
12+
<button id="copy-button">Copy Hex Code</button>
13+
{% endblock %}

0 commit comments

Comments
 (0)