Move HTML response to view package
This commit is contained in:
parent
c3b88979a8
commit
9666f104a2
3 changed files with 90 additions and 85 deletions
|
|
@ -7,6 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi_utils.tasks import repeat_every
|
||||
|
||||
from prophet import view
|
||||
from prophet.domain.improvement import Improvement
|
||||
from prophet.domain.improvement_repo import IImprovementRepo
|
||||
from prophet.domain.original import Original
|
||||
|
|
@ -64,21 +65,27 @@ def improve_originals(originals: list[Original]) -> list[Improvement]:
|
|||
return improvements
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
def init() -> FastAPI:
|
||||
app = FastAPI()
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
|
||||
origins = [
|
||||
origins = [
|
||||
"http://localhost",
|
||||
"http://localhost:8080",
|
||||
]
|
||||
]
|
||||
|
||||
app.add_middleware(
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
)
|
||||
view.define_routes(app)
|
||||
return app
|
||||
|
||||
|
||||
app = init()
|
||||
|
||||
|
||||
@app.get("/improve-title")
|
||||
|
|
@ -94,6 +101,7 @@ def improve_summary(original_title: str, new_title: str, original_summary: str):
|
|||
return llm.rewrite_summary(o, new_title)
|
||||
|
||||
|
||||
# TODO: Switch to lifecycle events to avoid deprecated method
|
||||
@app.on_event("startup")
|
||||
@repeat_every(seconds=REFRESH_PERIOD)
|
||||
async def refresh_articles():
|
||||
|
|
|
|||
|
|
@ -1,84 +1,69 @@
|
|||
# pyright: reportUnusedFunction=false
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
from prophet import app
|
||||
from prophet.domain.improvement_repo import IImprovementRepo
|
||||
from prophet.infra.improvement_pickle_repo import ImprovementPickleRepo
|
||||
|
||||
repo: IImprovementRepo = ImprovementPickleRepo()
|
||||
|
||||
html_app = app.app
|
||||
|
||||
|
||||
@html_app.get("/improvements", response_class=HTMLResponse)
|
||||
def list_improvements():
|
||||
def define_routes(app: FastAPI):
|
||||
@app.get("/improvements", response_class=HTMLResponse)
|
||||
def list_improvements():
|
||||
improved = repo.get_all()
|
||||
return (
|
||||
"""<button hx-get="/originals" hx-target="#content">Originals</button> """
|
||||
+ "\n".join(
|
||||
f"""
|
||||
<div class="card">
|
||||
<div class="card">
|
||||
<div class="card-img">
|
||||
<img src="{item.original.image_link if item.original.image_link else "https://placehold.co/300x200"}" width="600">
|
||||
</div>
|
||||
<div class="card-title">{item.title}</div>
|
||||
<div class="card-summary">{item.summary}</div>
|
||||
</div>"""
|
||||
for item in sorted(improved, key=lambda i: i.original.date, reverse=True)
|
||||
<div class="card-title">{item.title}</div>
|
||||
<div class="card-summary">{item.summary}</div>
|
||||
</div>"""
|
||||
for item in sorted(
|
||||
improved, key=lambda i: i.original.date, reverse=True
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@html_app.get("/originals", response_class=HTMLResponse)
|
||||
def list_originals():
|
||||
@app.get("/originals", response_class=HTMLResponse)
|
||||
def list_originals():
|
||||
improved = repo.get_all()
|
||||
return (
|
||||
"""<button hx-get="/improvements" hx-target="#content">Improvements</button> """
|
||||
+ "\n".join(
|
||||
f"""
|
||||
<div class="card">
|
||||
<div class="card">
|
||||
<div class="card-img">
|
||||
<img src="{item.original.image_link if item.original.image_link else "https://placehold.co/300x200"}" width="600">
|
||||
</div>
|
||||
<div class="card-title">{item.original.title}</div>
|
||||
<div class="card-summary">{item.original.summary}</div>
|
||||
</div>"""
|
||||
for item in sorted(improved, key=lambda i: i.original.date, reverse=True)
|
||||
</div>"""
|
||||
for item in sorted(
|
||||
improved, key=lambda i: i.original.date, reverse=True
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
style = """
|
||||
.card {
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
margin: auto;
|
||||
margin-bottom: 40px;
|
||||
width: 600px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 24px;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@html_app.get("/", response_class=HTMLResponse)
|
||||
def root_route():
|
||||
return f"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
def root_route():
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>The Pollen Prophet</title>
|
||||
<script src="https://unpkg.com/htmx.org@1.6.1"></script>
|
||||
<style>
|
||||
{style}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<link href="static/style.css" rel="stylesheet"
|
||||
</head>
|
||||
<body>
|
||||
<h1>The Pollen Prophet</h1>
|
||||
<h2>Making funny since 2025 what ought not bee.</h2>
|
||||
<div hx-get="/improvements" hx-target="#content" hx-trigger="load" id="content"></div>
|
||||
</body>
|
||||
</html>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
|
|
|||
12
static/style.css
Normal file
12
static/style.css
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
.card {
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
margin: auto;
|
||||
margin-bottom: 40px;
|
||||
width: 600px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 24px;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue