Skip to content

Commit 88dedaf

Browse files
feat: mvp
Correctly render the quotes and authors w/css and htmx
1 parent 42d3cda commit 88dedaf

File tree

6 files changed

+81
-7
lines changed

6 files changed

+81
-7
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@ COPY . .
3232

3333
EXPOSE 8000
3434

35-
ENTRYPOINT [ "gunicorn" ]
36-
CMD ["-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000", "app:app", "--reload"]
35+
ENTRYPOINT [ "/app/startup.sh" ]

app.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
#!/usr/bin/env python
22

33
import sqlite3
4-
from fastapi import FastAPI, Query,Request
4+
from fastapi import FastAPI, Query, Request, status
5+
from fastapi.middleware.cors import CORSMiddleware
56
from fastapi.responses import HTMLResponse, JSONResponse
7+
from fastapi.staticfiles import StaticFiles
68
from fastapi.templating import Jinja2Templates
7-
from typing import List, Optional
9+
from typing import Optional
810

911
app = FastAPI()
12+
app.add_middleware(
13+
CORSMiddleware,
14+
allow_origins=["*"],
15+
allow_methods=["*"],
16+
allow_headers=["*"],
17+
)
18+
app.mount("/static", StaticFiles(directory="static"), name="static")
1019
templates = Jinja2Templates(directory="templates")
1120

1221

13-
@app.get('/')
22+
@app.get('/', response_class=HTMLResponse)
1423
def home(request: Request):
1524
return templates.TemplateResponse("index.html", {"request": request})
1625

@@ -41,6 +50,8 @@ def get_quotes_by_limit(request: Request, limit: int) -> JSONResponse:
4150
db = sqlite3.connect("db.sqlite")
4251
c = db.cursor()
4352
c.execute(f"SELECT * FROM quotes ORDER BY RANDOM() LIMIT {limit}")
44-
quotes = c.fetchall()
53+
quotes = [{"id": id,
54+
"quote": quote,
55+
"author": author} for id, quote, author in c.fetchall()]
4556
db.close()
4657
return JSONResponse(content={"quotes": quotes})

startup.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
gunicorn \
4+
-k uvicorn.workers.UvicornWorker \
5+
-b 0.0.0.0:8000 \
6+
--reload \
7+
--reload-extra-file="templates/index.html" \
8+
--reload-extra-file="static/styles.css" \
9+
app:app

static/.gitkeep

Whitespace-only changes.

static/styles.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
body {
2+
display: flex;
3+
flex-direction: column;
4+
justify-content: center;
5+
align-items: center;
6+
height: 100vh;
7+
margin: 0;
8+
background-color: lightblue;
9+
}
10+
11+
#quote-placeholder {
12+
height: 100px;
13+
}
14+
15+
#quote-display {
16+
position: absolute;
17+
/* 50% is the vertical center of the viewport, and 50px is half the height of the #quote-placeholder div */
18+
top: calc(50% + 50px);
19+
left: 0;
20+
right: 0;
21+
bottom: 0;
22+
}
23+
24+
#quote-text {
25+
color: black;
26+
font-size: 2em;
27+
padding-top: 10px;
28+
}
29+
30+
#author-text {
31+
color: rgb(45, 45, 45);
32+
font-size: 1.5em;
33+
padding-top: 20px;
34+
}

templates/index.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,34 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>QaaS</title>
88
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.16/dist/tailwind.min.css" rel="stylesheet">
9+
<link href="/static/styles.css" rel="stylesheet">
10+
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
911
</head>
1012

1113
<body class="bg-gray-200">
1214
<div class="container mx-auto">
1315
<h1 class="text-4xl text-center py-5">Welcome to QaaS</h1>
14-
<p class="text-center">This is a boilerplate index page rendered by FastAPI and styled with Tailwind CSS.</p>
16+
<p class="text-center">Quotes as a Service is happy to serve 🤙</p>
17+
<br>
18+
<div class="flex justify-center">
19+
<button hx-get="/quotes/1" hx-trigger="click" hx-target="#quote-display"
20+
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Get a Quote</button>
21+
</div>
22+
<div id="quote-placeholder" class="text-center mt-4">
23+
<!-- This is a placeholder for the quote -->
24+
</div>
25+
<div id="quote-display" class="text-center mt-4">
26+
<!-- The quote will be inserted here -->
27+
</div>
1528
</div>
29+
<script>
30+
document.body.addEventListener('htmx:afterSwap', function (event) {
31+
var quotes = JSON.parse(event.detail.xhr.response).quotes;
32+
var quote = quotes[0]; // Get the first quote object from the array
33+
var quoteElement = document.getElementById('quote-display');
34+
quoteElement.innerHTML = `<p id="quote-text" class="text-center text-2xl">${quote.quote}</p><p id="author-text" class="text-center text-xl">- ${quote.author}</p>`;
35+
});
36+
</script>
1637
</body>
1738

1839
</html>

0 commit comments

Comments
 (0)