Skip to content

Commit e0be0ec

Browse files
authored
Merge branch 'master' into solid-principles-python
2 parents 79344e3 + 87935df commit e0be0ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1205
-1
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 %}

how-to-indent-in-python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Properly Indent Python Code
2+
3+
This folder contains sample code for the Real Python tutorial [How to Properly Indent Python Code](https://realpython.com/how-to-indent-in-python/).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
long_list_of_names = [
2+
"Amy",
3+
"Brian",
4+
"Carol",
5+
"Dennis",
6+
"Emma",
7+
"Frank",
8+
"Georgia",
9+
"Herbert",
10+
"Isabelle",
11+
"Joshua",
12+
"Kimberly",
13+
"Laurence",
14+
"Megan",
15+
"Nicolas",
16+
"Ophelia",
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lucky_number = 7
2+
for number in range(10):
3+
if number == lucky_number:
4+
print("Found the lucky number!")
5+
else:
6+
print(f"{number} is not my lucky number.")
7+
8+
print("Done.")

0 commit comments

Comments
 (0)