|
|
from fastapi import FastAPI |
|
|
from fastapi.responses import HTMLResponse |
|
|
|
|
|
app = FastAPI() |
|
|
state = [-1,-1,-1,-1,-1,-1,-1,-1,-1] |
|
|
step = '🐱' |
|
|
|
|
|
@app.get("/") |
|
|
def greet_json(): |
|
|
|
|
|
|
|
|
html_content = """ |
|
|
<html> |
|
|
<head> |
|
|
<title>Some HTML in here</title> |
|
|
<style> |
|
|
.char{ |
|
|
padding:20px; |
|
|
background-color:green; |
|
|
margin-left:55px; |
|
|
margin-right:55px; |
|
|
cursor:pointer; |
|
|
} |
|
|
.char:even { |
|
|
background-color:yellow; |
|
|
} |
|
|
</style> |
|
|
<script |
|
|
src="https://code.jquery.com/jquery-3.7.1.min.js" |
|
|
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" |
|
|
crossorigin="anonymous"></script> |
|
|
|
|
|
</head> |
|
|
<body> |
|
|
<script> |
|
|
function go(i){ |
|
|
$.get('/turn/'+i); |
|
|
document.location.reload(); |
|
|
} |
|
|
</script> |
|
|
""" |
|
|
html_content += 'сейчас ходит: ' + step + '<br><br>' |
|
|
for i in range(9): |
|
|
if state[i] == -1: |
|
|
char = '_' |
|
|
elif state[i] == 1: |
|
|
char = '🐭' |
|
|
elif state[i] == 0: |
|
|
char = '🐱' |
|
|
|
|
|
if i in [0,3,6]: |
|
|
html_content += '<br><br><br><br>' |
|
|
html_content += '<span class="char"><span onclick="go('+str(i)+')">' + char + '</span></span>' |
|
|
|
|
|
html_content += '<br><br><br><br><br><a href="/reset">reset</a>' |
|
|
|
|
|
html_content += """ |
|
|
</body> |
|
|
</html> |
|
|
""" |
|
|
return HTMLResponse(content=html_content, status_code=200) |
|
|
|
|
|
def change_step(): |
|
|
global step |
|
|
print(step) |
|
|
if step == '🐱': |
|
|
step = '🐭' |
|
|
elif step == '🐭': |
|
|
step = '🐱' |
|
|
|
|
|
@app.get('/turn/{pos}') |
|
|
def turn(pos): |
|
|
global state |
|
|
|
|
|
if state[int(pos)] == '-1': |
|
|
state[int(pos)] = step |
|
|
else: |
|
|
state[int(pos)] = int(not state[int(pos)]) |
|
|
change_step() |
|
|
|
|
|
@app.get('/reset') |
|
|
def reset(): |
|
|
global state |
|
|
state = [-1,-1,-1,-1,-1,-1,-1,-1,-1] |