|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="it"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"/> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| 6 | + <meta name="description" |
| 7 | + content="Capitolo 21 di Python Zero to Hero: crea applicazioni web e API RESTful con Flask e FastAPI—routing, template, gestione delle richieste e deployment."/> |
| 8 | + <meta name="keywords" |
| 9 | + content="Python, sviluppo web, Flask, FastAPI, routing, template, Jinja2, dependency injection, deployment"/> |
| 10 | + <meta name="author" content="Luca Bocaletto"/> |
| 11 | + <title>Capitolo 21 – Sviluppo Web con Flask & FastAPI | Python Zero to Hero</title> |
| 12 | + |
| 13 | + <!-- Bootstrap 5 CSS --> |
| 14 | + <link |
| 15 | + href=" https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" |
| 16 | + rel="stylesheet" |
| 17 | + crossorigin="anonymous" |
| 18 | + /> |
| 19 | + |
| 20 | + <!-- Highlight.js CSS --> |
| 21 | + <link |
| 22 | + rel="stylesheet" |
| 23 | + href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" |
| 24 | + crossorigin="anonymous" |
| 25 | + referrerpolicy="no-referrer" |
| 26 | + /> |
| 27 | + |
| 28 | + <style> |
| 29 | + body { padding-top: 4.5rem; } |
| 30 | + pre code { font-size: .9rem; } |
| 31 | + .btn-py { font-family: monospace; } |
| 32 | + </style> |
| 33 | +</head> |
| 34 | +<body> |
| 35 | + <!-- Navbar --> |
| 36 | + <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top"> |
| 37 | + <div class="container"> |
| 38 | + <a class="navbar-brand" href="index.html">Python Zero to Hero</a> |
| 39 | + <button class="navbar-toggler" type="button" data-bs-toggle="collapse" |
| 40 | + data-bs-target="#navContent" aria-controls="navContent" |
| 41 | + aria-expanded="false" aria-label="Toggle navigation"> |
| 42 | + <span class="navbar-toggler-icon"></span> |
| 43 | + </button> |
| 44 | + <div class="collapse navbar-collapse" id="navContent"> |
| 45 | + <ul class="navbar-nav ms-auto"> |
| 46 | + <li class="nav-item"><a class="nav-link" href="chapter20.html">Capitolo 20</a></li> |
| 47 | + <li class="nav-item"><a class="nav-link" href="index.html">Indice</a></li> |
| 48 | + <li class="nav-item"><a class="nav-link" href="chapter22.html">Capitolo 22</a></li> |
| 49 | + <li class="nav-item"> |
| 50 | + <a class="nav-link" |
| 51 | + href="https://github.com/bocaletto-luca/python-zero-to-hero" |
| 52 | + target="_blank">Repo GitHub</a> |
| 53 | + </li> |
| 54 | + </ul> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </nav> |
| 58 | + |
| 59 | + <!-- Contenuto principale --> |
| 60 | + <main class="container"> |
| 61 | + <!-- Intestazione --> |
| 62 | + <header class="my-4"> |
| 63 | + <h1 class="display-6">Capitolo 21: Sviluppo Web con Flask & FastAPI</h1> |
| 64 | + <p class="text-muted"> |
| 65 | + Impara a creare applicazioni web e API RESTful usando Flask e FastAPI, dal routing alla templating fino al deployment. |
| 66 | + </p> |
| 67 | + <a href="src/chapter21.py" download class="btn btn-outline-primary btn-sm btn-py"> |
| 68 | + Scarica <code>chapter21.py</code> |
| 69 | + </a> |
| 70 | + <hr> |
| 71 | + </header> |
| 72 | + |
| 73 | + <!-- Obiettivi --> |
| 74 | + <section id="objectives" class="mb-5"> |
| 75 | + <h2>Obiettivi</h2> |
| 76 | + <ul> |
| 77 | + <li>Configurare un’app Flask di base: route, view, template.</li> |
| 78 | + <li>Gestire metodi HTTP, parametri di query e dati di form.</li> |
| 79 | + <li>Costruire API REST con FastAPI: parametri path e body, validazione.</li> |
| 80 | + <li>Usare Jinja2 per template HTML dinamici.</li> |
| 81 | + <li>Distribuire un’app Flask/FastAPI con Uvicorn o Gunicorn.</li> |
| 82 | + </ul> |
| 83 | + </section> |
| 84 | + |
| 85 | + <!-- 1. Flask Basics --> |
| 86 | + <section id="flask-basics" class="mb-5"> |
| 87 | + <h2>1. Nozioni di base su Flask</h2> |
| 88 | + <pre><code class="python">from flask import Flask, request, render_template |
| 89 | + |
| 90 | +app = Flask(__name__) |
| 91 | + |
| 92 | +@app.route("/") |
| 93 | +def index(): |
| 94 | + return "Hello, Flask!" |
| 95 | + |
| 96 | +@app.route("/greet/<name>") |
| 97 | +def greet(name): |
| 98 | + return f"Benvenuto, {name}!" |
| 99 | + |
| 100 | +@app.route("/submit", methods=["GET","POST"]) |
| 101 | +def submit(): |
| 102 | + if request.method == "POST": |
| 103 | + data = request.form.get("data") |
| 104 | + return render_template("result.html", data=data) |
| 105 | + return render_template("form.html") |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + app.run(debug=True) |
| 109 | +</code></pre> |
| 110 | + <p>Struttura della cartella dei template:</p> |
| 111 | + <pre><code class="bash">templates/ |
| 112 | +├── form.html |
| 113 | +└── result.html</code></pre> |
| 114 | + <p>Esempio di <code>form.html</code>:</p> |
| 115 | + <pre><code class="html"><form method="post"> |
| 116 | + <input name="data" /> |
| 117 | + <button>Invia</button> |
| 118 | +</form></code></pre> |
| 119 | + <p>Esempio di <code>result.html</code>:</p> |
| 120 | + <pre><code class="html"><p>Hai inviato: {{ data }}</p></code></pre> |
| 121 | + <hr> |
| 122 | + </section> |
| 123 | + |
| 124 | + <!-- 2. FastAPI Basics --> |
| 125 | + <section id="fastapi-basics" class="mb-5"> |
| 126 | + <h2>2. Nozioni di base su FastAPI</h2> |
| 127 | + <pre><code class="python">from fastapi import FastAPI |
| 128 | +from pydantic import BaseModel |
| 129 | + |
| 130 | +class Item(BaseModel): |
| 131 | + name: str |
| 132 | + price: float |
| 133 | + |
| 134 | +app = FastAPI() |
| 135 | + |
| 136 | +@app.get("/items/{item_id}") |
| 137 | +def read_item(item_id: int, q: str | None = None): |
| 138 | + return {"item_id": item_id, "q": q} |
| 139 | + |
| 140 | +@app.post("/items/") |
| 141 | +def create_item(item: Item): |
| 142 | + return {"message": "Creato", "item": item} |
| 143 | + |
| 144 | +# esegui con: uvicorn src.chapter21:app --reload</code></pre> |
| 145 | + <hr> |
| 146 | + </section> |
| 147 | + |
| 148 | + <!-- 3. Deployment --> |
| 149 | + <section id="deployment" class="mb-5"> |
| 150 | + <h2>3. Deployment</h2> |
| 151 | + <pre><code class="bash"># Gunicorn per Flask |
| 152 | +gunicorn --bind 0.0.0.0:8000 wsgi:app |
| 153 | + |
| 154 | +# Uvicorn per FastAPI |
| 155 | +uvicorn src.chapter21:app --host 0.0.0.0 --port 8000</code></pre> |
| 156 | + </section> |
| 157 | + |
| 158 | + <!-- Esercizi --> |
| 159 | + <section id="exercises" class="mb-5"> |
| 160 | + <h2>Esercizi</h2> |
| 161 | + <ol> |
| 162 | + <li>Crea un’app “to-do” in Flask con aggiunta/rimozione di task in memoria.</li> |
| 163 | + <li>Scrivi un endpoint FastAPI che valida un payload JSON di dati utente.</li> |
| 164 | + <li>Usa l’ereditarietà dei template Jinja2 per costruire un layout comune nelle pagine Flask.</li> |
| 165 | + <li>Containerizza la tua app Flask/FastAPI con Docker ed eseguila localmente.</li> |
| 166 | + </ol> |
| 167 | + </section> |
| 168 | + |
| 169 | + <!-- Navigazione --> |
| 170 | + <nav class="d-flex justify-content-between mb-5"> |
| 171 | + <a href="chapter20.html" class="btn btn-outline-secondary">← Capitolo 20</a> |
| 172 | + <a href="chapter22.html" class="btn btn-primary">Capitolo 22 →</a> |
| 173 | + </nav> |
| 174 | + </main> |
| 175 | + |
| 176 | + <!-- Footer --> |
| 177 | + <footer class="text-center py-4 border-top"> |
| 178 | + <small> |
| 179 | + © 2025 Python Zero to Hero • |
| 180 | + <a href="https://github.com/bocaletto-luca/python-zero-to-hero" target="_blank"> |
| 181 | + bocaletto-luca/python-zero-to-hero |
| 182 | + </a> |
| 183 | + </small> |
| 184 | + </footer> |
| 185 | + |
| 186 | + <!-- Bootstrap JS Bundle --> |
| 187 | + <script |
| 188 | + src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" |
| 189 | + crossorigin="anonymous" |
| 190 | + ></script> |
| 191 | + <!-- Highlight.js JS --> |
| 192 | + <script |
| 193 | + src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js" |
| 194 | + crossorigin="anonymous" |
| 195 | + referrerpolicy="no-referrer" |
| 196 | + ></script> |
| 197 | + <script>hljs.highlightAll();</script> |
| 198 | +</body> |
| 199 | +</html> |
0 commit comments