added: dummy ui for FastAPI

uebung_entities
Felix Jan Michael Mucha 2025-01-05 17:06:49 +01:00
parent c49874c572
commit 3f3c13abd0
7 changed files with 89 additions and 34 deletions

View File

@ -1,27 +0,0 @@
from flask import Flask, render_template, redirect, url_for
app = Flask(__name__)
@app.route('/')
def home():
return redirect(url_for('products'))
@app.route('/cart')
def cart():
cart_items = [
{'name': 'Item 1', 'quantity': 2},
{'name': 'Item 2', 'quantity': 1}
]
return render_template('cart.html', cart_items=cart_items)
@app.route('/products')
def products():
products = [
{'name': 'Product 1', 'price': 2.5},
{'name': 'Product 2', 'price': 1.5}
]
return render_template('product.html', products=products)
if __name__ == '__main__':
app.run(debug=True, port=5000)

View File

@ -0,0 +1,19 @@
body {
font-family: Arial, sans-serif;
}
header {
background-color: #f8f9fa;
padding: 1rem;
text-align: center;
}
main {
padding: 1rem;
}
button {
padding: 0.5rem 1rem;
margin-top: 1rem;
cursor: pointer;
}

View File

@ -0,0 +1,3 @@
document.addEventListener('DOMContentLoaded', function() {
console.log('JavaScript is loaded');
});

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}My Shop{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<link rel="stylesheet" href="{{ url_for('custom_static', filename='css/style.css') }}">
</head>
<body>
<header>
@ -13,6 +13,6 @@
<main>
{% block content %}{% endblock %}
</main>
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
<script src="{{ url_for('custom_static', filename='js/script.js') }}"></script>
</body>
</html>

View File

@ -9,4 +9,7 @@
<li>{{ item.name }} - {{ item.quantity }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('ui') }}" class="btn btn-primary">Back to the prodcuts</a>
{% endblock %}

View File

@ -10,5 +10,6 @@
{% endfor %}
</ul>
<a href="{{ url_for('cart') }}" class="btn btn-primary">Go to Cart</a>
<a href="{{ url_for('ui_view_cart') }}" class="btn btn-primary">Go to Cart</a>
{% endblock %}

View File

@ -1,11 +1,67 @@
from interface_adapters.controllers.cart_controller import router as cart_router
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse
from fastapi.responses import HTMLResponse
from fastapi.responses import FileResponse
from fastapi.requests import Request
from fastapi.exceptions import HTTPException
from fastapi import FastAPI
import uvicorn
import os
app = FastAPI()
app.include_router(cart_router)
@app.get("/")
async def root():
return {"message": "Hello World"}
# Allow CORS for all origins (for simplicity)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Serve static files
# TODO fix this
#app.mount("/static", StaticFiles(directory="framework_driver/ui/static"), name="static")
# TODO so this is not needed
@app.get("/custom_static/{filename:path}", response_class=FileResponse, name="custom_static", include_in_schema=False)
async def custom_static(filename: str):
file_path = os.path.join("framework_driver/ui/static", filename)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(file_path)
# Set up templates
templates = Jinja2Templates(directory="framework_driver/ui/templates")
@app.get("/", include_in_schema=False)
async def root():
return RedirectResponse(url="/docs")
@app.get("/ui", response_class=HTMLResponse)
async def ui(request: Request):
products = [
{"id": 1, "name": "Product 1", "price": 10.0},
{"id": 2, "name": "Product 2", "price": 20.0}
]
return templates.TemplateResponse("products.html", {"request": request, "products": products})
@app.get("/ui/view_cart", response_class=HTMLResponse, name="ui_view_cart")
async def ui_view_cart(request: Request):
# Dummy cart data for demonstration
cart_items = [
{"product_id": 1, "name": "Product 1", "quantity": 2},
{"product_id": 2, "name": "Product 2", "quantity": 1}
]
return templates.TemplateResponse("cart.html", {"request": request, "cart_items": cart_items})
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)