Create Template for displaying Images

This commit is contained in:
shadowik 2024-10-15 02:33:16 +04:00
parent 1e6783b476
commit 20379f8d37
7 changed files with 169 additions and 4 deletions

View File

@ -7,8 +7,6 @@ from datetime import datetime
DATABASE_URL = "mysql+pymysql://wind:wind@193.124.203.110:3306/wind_towers"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

View File

@ -1,13 +1,23 @@
from fastapi import FastAPI, Depends
from fastapi.responses import RedirectResponse
from fastapi import FastAPI, Depends, Request
from fastapi.responses import RedirectResponse, HTMLResponse
from sqlalchemy.orm import Session
from sqlalchemy.future import select
from database import SessionLocal, User, Base, engine
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from src.floris_router import router as floris_router
from src.floris_router import get_images
Base.metadata.create_all(bind=engine)
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/public", StaticFiles(directory="public"), name="public")
app.include_router(floris_router)
def get_db():
@ -27,12 +37,22 @@ async def hello():
async def redirect_to_docs():
return RedirectResponse(url="/docs")
@app.get("/users")
def read_users(db: Session = Depends(get_db)):
result = db.execute(select(User))
users = result.scalars().all() # Получаем всех пользователей
return users
@app.get("/main", response_class=HTMLResponse)
async def main(request: Request):
params = {"request": request}
params.update(await get_images())
return templates.TemplateResponse("main.html", params)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8080) # Изменено на localhost

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -0,0 +1,36 @@
from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import FileResponse
STATIC_DIR = Path("public/floris")
router = APIRouter(
prefix="/floris",
tags=["Static Files from Floris"],
)
@router.get("/get_images")
async def get_images():
try:
images = [file.name for file in STATIC_DIR.iterdir() if file.is_file()]
return {"images": images}
except Exception as e:
raise HTTPException(
status_code=500,
detail=str(e)
)
@router.get("/download_image/{image_name}")
async def download_image(
image_name: str
):
image_path = STATIC_DIR / image_name
if not image_path.exists() or not image_path.is_file():
raise HTTPException(status_code=404, detail="Image not found")
return FileResponse(image_path, media_type="image/jpeg", filename=image_name)

View File

@ -0,0 +1 @@
st

67
server/static/style.css Normal file
View File

@ -0,0 +1,67 @@
body {
font-family: Arial, sans-serif;
background-color: #f3f4f6;
color: #333;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.image-viewer {
background-color: #fff5e6;
border-radius: 8px;
padding: 20px;
margin-right: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.image-viewer img {
max-width: 400px;
max-height: 400px;
display: block;
margin-bottom: 10px;
}
.image-list {
background-color: #e6f7ff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.image-list ul {
list-style-type: none;
padding: 0;
}
.image-list li {
margin: 5px 0;
padding: 5px;
cursor: pointer;
background-color: #fff;
border-radius: 4px;
transition: background-color 0.3s;
}
.image-list li:hover {
background-color: #cceeff;
}
button {
background-color: #82b1ff;
border: none;
color: white;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #6699ff;
}

View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Viewer</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<div class="container">
<div class="image-viewer">
<img id="display-image" src="/public/floris/Yaw_example.png" alt="Selected Image">
<button onclick="generateImage()">Generate Image</button>
<button onclick="downloadImage()">Download Image</button>
</div>
<div class="image-list">
<h3>Available Images</h3>
<ul id="image-list">
{% for image in images %}
<li onclick="selectImage('{{ image }}')">{{ image }}</li>
{% endfor %}
</ul>
</div>
</div>
<script>
function selectImage(imageName) {
const imgElement = document.getElementById('display-image');
imgElement.src = `/public/floris/${imageName}`;
}
function generateImage() {
alert("Image generation functionality not implemented yet.");
}
function downloadImage() {
const imgElement = document.getElementById('display-image');
const imageName = imgElement.src.split('/').pop();
window.location.href = `/floris/download_image/${imageName}`;
}
</script>
</body>
</html>