Spaces:
Running
Running
Commit
·
620876b
1
Parent(s):
4af4cb2
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,16 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 4 |
import chess
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 8 |
|
| 9 |
-
@app.get("/"
|
| 10 |
-
def read_root():
|
| 11 |
-
return """
|
| 12 |
-
<!DOCTYPE html>
|
| 13 |
-
<html>
|
| 14 |
-
<head>
|
| 15 |
-
<link rel="stylesheet" href="/static/chessboard-1.0.0.min.css">
|
| 16 |
-
<style>
|
| 17 |
-
body { font-family: Arial; max-width: 800px; margin: 50px auto; }
|
| 18 |
-
#board { width: 400px; margin: 20px 0; }
|
| 19 |
-
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
|
| 20 |
-
#fen, #result { background: #f0f0f0; padding: 10px; margin: 10px 0; font-family: monospace; }
|
| 21 |
-
</style>
|
| 22 |
-
</head>
|
| 23 |
-
<body>
|
| 24 |
-
<h1>Chess Position Search</h1>
|
| 25 |
-
<div id="board"></div>
|
| 26 |
-
<button onclick="board.start()">Reset</button>
|
| 27 |
-
<button onclick="board.clear()">Clear</button>
|
| 28 |
-
<button onclick="search()">Search</button>
|
| 29 |
-
<div id="fen"></div>
|
| 30 |
-
<div id="result"></div>
|
| 31 |
-
|
| 32 |
-
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
| 33 |
-
<script src="/static/chessboard-1.0.0.min.js"></script>
|
| 34 |
-
<script>
|
| 35 |
-
var board = Chessboard('board', {
|
| 36 |
-
draggable: true,
|
| 37 |
-
dropOffBoard: 'trash',
|
| 38 |
-
sparePieces: true,
|
| 39 |
-
position: 'start',
|
| 40 |
-
pieceTheme: '/static/img/chesspieces/wikipedia/{piece}.png',
|
| 41 |
-
onChange: () => document.getElementById('fen').textContent = 'FEN: ' + board.fen()
|
| 42 |
-
});
|
| 43 |
-
|
| 44 |
-
function search() {
|
| 45 |
-
fetch('/search', {
|
| 46 |
-
method: 'POST',
|
| 47 |
-
headers: {'Content-Type': 'application/json'},
|
| 48 |
-
body: JSON.stringify({fen: board.fen()})
|
| 49 |
-
})
|
| 50 |
-
.then(r => r.json())
|
| 51 |
-
.then(d => document.getElementById('result').textContent = JSON.stringify(d, null, 2));
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
document.getElementById('fen').textContent = 'FEN: ' + board.fen();
|
| 55 |
-
</script>
|
| 56 |
-
</body>
|
| 57 |
-
</html>
|
| 58 |
-
"""
|
| 59 |
|
| 60 |
@app.post("/search")
|
| 61 |
async def search(data: dict):
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from fastapi.templating import Jinja2Templates
|
| 5 |
import chess
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 9 |
+
templates = Jinja2Templates(directory="templates")
|
| 10 |
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def read_root(request: Request):
|
| 13 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.post("/search")
|
| 16 |
async def search(data: dict):
|