christopher commited on
Commit
9b0851e
·
1 Parent(s): adc9994

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -1,10 +1,9 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.staticfiles import StaticFiles
 
4
 
5
  app = FastAPI()
6
-
7
- # Mount static files - THIS IS REQUIRED
8
  app.mount("/static", StaticFiles(directory="static"), name="static")
9
 
10
  @app.get("/", response_class=HTMLResponse)
@@ -14,19 +13,57 @@ def read_root():
14
  <html>
15
  <head>
16
  <link rel="stylesheet" href="/static/chessboard-1.0.0.min.css">
 
 
 
 
 
 
17
  </head>
18
  <body>
19
  <h1>Chess Position Search</h1>
20
- <div id="board" style="width: 400px"></div>
 
 
 
 
 
21
 
22
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
23
  <script src="/static/chessboard-1.0.0.min.js"></script>
24
  <script>
25
  var board = Chessboard('board', {
 
 
 
26
  position: 'start',
27
- pieceTheme: '/static/img/chesspieces/wikipedia/{piece}.png'
 
28
  });
 
 
 
 
 
 
 
 
 
 
 
 
29
  </script>
30
  </body>
31
  </html>
32
- """
 
 
 
 
 
 
 
 
 
 
 
 
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("/", response_class=HTMLResponse)
 
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):
62
+ board = chess.Board(data['fen'])
63
+ return {
64
+ "fen": data['fen'],
65
+ "valid": board.is_valid(),
66
+ "turn": "white" if board.turn else "black",
67
+ "legal_moves": len(list(board.legal_moves)),
68
+ "ascii": str(board)
69
+ }