Esimerkkiprojektit: täysi CRUD + HTML UI root-osoitteessa
Few-shot esimerkit päivitetty:
- main.py: GET/POST/PUT/DELETE + FileResponse("/") index.html:lle
- index.html: yksinkertainen UI fetch()-kutsuilla API:in
- /api/ -prefiksi JSON-endpointeille
- Esimerkkipromptit kuvaavat CRUD-operaatiot eksplisiittisesti
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2450,23 +2450,73 @@ IMPORTANT: Only list pip-installable packages. NEVER include Python stdlib modul
|
||||
}
|
||||
|
||||
const coderExample = file.name.includes('main') || file.name.includes('app')
|
||||
? `\nEXAMPLE output for a main.py:
|
||||
from fastapi import FastAPI, Depends
|
||||
? `\nEXAMPLE output for a main.py (CRUD + HTML UI):
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from models import get_db, User
|
||||
from models import get_db, Base, engine, User
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/users")
|
||||
@app.get("/")
|
||||
def index():
|
||||
return FileResponse("index.html")
|
||||
|
||||
@app.get("/api/users")
|
||||
def list_users(db: Session = Depends(get_db)):
|
||||
return db.query(User).all()
|
||||
|
||||
@app.post("/users")
|
||||
@app.post("/api/users")
|
||||
def create_user(name: str, db: Session = Depends(get_db)):
|
||||
user = User(name=name)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
return {"id": user.id, "name": user.name}`
|
||||
return {"id": user.id, "name": user.name}
|
||||
|
||||
@app.put("/api/users/{user_id}")
|
||||
def update_user(user_id: int, name: str, db: Session = Depends(get_db)):
|
||||
user = db.query(User).get(user_id)
|
||||
if not user: raise HTTPException(404)
|
||||
user.name = name
|
||||
db.commit()
|
||||
return {"id": user.id, "name": user.name}
|
||||
|
||||
@app.delete("/api/users/{user_id}")
|
||||
def delete_user(user_id: int, db: Session = Depends(get_db)):
|
||||
user = db.query(User).get(user_id)
|
||||
if not user: raise HTTPException(404)
|
||||
db.delete(user)
|
||||
db.commit()
|
||||
return {"ok": True}`
|
||||
: file.name.includes('index.html')
|
||||
? `\nEXAMPLE output for index.html (simple CRUD UI):
|
||||
<!DOCTYPE html>
|
||||
<html><head><title>App</title></head>
|
||||
<body>
|
||||
<h1>Users</h1>
|
||||
<input id="name" placeholder="Name"><button onclick="add()">Add</button>
|
||||
<ul id="list"></ul>
|
||||
<script>
|
||||
async function load() {
|
||||
const r = await fetch('/api/users');
|
||||
const users = await r.json();
|
||||
document.getElementById('list').innerHTML = users.map(u =>
|
||||
'<li>' + u.name + ' <button onclick="del('+u.id+')">x</button></li>'
|
||||
).join('');
|
||||
}
|
||||
async function add() {
|
||||
const name = document.getElementById('name').value;
|
||||
await fetch('/api/users?name='+name, {method:'POST'});
|
||||
document.getElementById('name').value = '';
|
||||
load();
|
||||
}
|
||||
async function del(id) {
|
||||
await fetch('/api/users/'+id, {method:'DELETE'});
|
||||
load();
|
||||
}
|
||||
load();
|
||||
</script></body></html>`
|
||||
: file.name.includes('model')
|
||||
? `\nEXAMPLE output for a models.py:
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Text
|
||||
@@ -2490,7 +2540,7 @@ def get_db():
|
||||
: '';
|
||||
const coderPrompt = `${context}Project: ${task}
|
||||
Write ONLY the file "${file.name}"${file.desc ? ': ' + file.desc : ''}.${extraInstructions}${coderExample}
|
||||
IMPORTANT: Keep the code SHORT. Max ~50 lines. No comments, no docstrings. Write minimal, working code. Output ONLY code.`;
|
||||
IMPORTANT: Keep the code SHORT. Max ~60 lines. No comments, no docstrings. Write minimal, working code. Output ONLY code. Serve index.html at / using FileResponse. Use /api/ prefix for JSON endpoints.`;
|
||||
const code = await kpnRun(agentPrompts.coder.model, coderPrompt);
|
||||
if (!code) {
|
||||
termLog(` ✗ Pipeline keskeytyi (${file.name})`, '#f85149');
|
||||
@@ -3197,7 +3247,7 @@ ${filesHtml}
|
||||
'kpn run coder-3b': ['"binary search tree in rust"', '"REST API with Flask"', '"async web scraper in python"'],
|
||||
'kpn run manager': ['"suunnittele REST API"', '"priorisoi tiimin tehtävät"'],
|
||||
'kpn run tester': ['"testaa login-toiminto"'],
|
||||
'kpn project': ['"FastAPI + SQLite REST API for users with HTML UI at /"', '"Flask todo app with Jinja2 templates and form UI"', '"FastAPI bookmark manager with HTML frontend and search"'],
|
||||
'kpn project': ['"FastAPI + SQLite CRUD API for users (create, list, update, delete) with HTML form UI served at /"', '"FastAPI todo app with SQLite: CRUD (add, list, toggle done, delete) with simple HTML UI at /"', '"FastAPI bookmark manager with SQLite: CRUD (add, list, edit, delete) with HTML search UI at /"'],
|
||||
'kpn pipeline': ['"rakenna todo-sovellus"', '"tee laskin pythonilla"'],
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user