Explorar el Código

Add a template engine

jherve hace 1 año
padre
commit
10de53bfa4
Se han modificado 5 ficheros con 26 adiciones y 5 borrados
  1. 1 1
      pdm.lock
  2. 1 0
      pyproject.toml
  3. 12 4
      src/de_quoi_parle_le_monde/web.py
  4. 3 0
      static/style.css
  5. 9 0
      templates/index.html

+ 1 - 1
pdm.lock

@@ -5,7 +5,7 @@
 groups = ["default"]
 strategy = ["cross_platform", "inherit_metadata"]
 lock_version = "4.4.1"
-content_hash = "sha256:5ec4c681709b597c4450450d154ac9e999580b1ac4af72ff4435c2961170c977"
+content_hash = "sha256:7709df78d511041ea4804080b95671dd5d982aa05a9932e77ad18827298744ee"
 
 [[package]]
 name = "aioboto3"

+ 1 - 0
pyproject.toml

@@ -21,6 +21,7 @@ dependencies = [
     "sentence-transformers>=2.6.1",
     "hypercorn>=0.16.0",
     "fastapi>=0.110.1",
+    "jinja2>=3.1.3",
 ]
 requires-python = "==3.11.*"
 readme = "README.md"

+ 12 - 4
src/de_quoi_parle_le_monde/web.py

@@ -1,8 +1,16 @@
-from fastapi import FastAPI
+from fastapi import FastAPI, Request
+from fastapi.responses import HTMLResponse
+from fastapi.staticfiles import StaticFiles
+from fastapi.templating import Jinja2Templates
 
 app = FastAPI()
 
 
-@app.get("/")
-async def read_root():
-    return {"Hello": "World"}
+app.mount("/static", StaticFiles(directory="static"), name="static")
+
+templates = Jinja2Templates(directory="templates")
+
+
+@app.get("/", response_class=HTMLResponse)
+async def read_root(request: Request):
+    return templates.TemplateResponse(request=request, name="index.html", context={})

+ 3 - 0
static/style.css

@@ -0,0 +1,3 @@
+h1 {
+    color: darkturquoise;
+}

+ 9 - 0
templates/index.html

@@ -0,0 +1,9 @@
+<html>
+<head>
+    <title>Hello</title>
+    <link href="{{ url_for('static', path='/style.css') }}" rel="stylesheet">
+</head>
+<body>
+    <h1>Hello World !</h1>
+</body>
+</html>