-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (82 loc) · 3.06 KB
/
main.py
File metadata and controls
91 lines (82 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import random
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/", response_class=HTMLResponse)
async def index():
return '''
<html>
<head>
<title>Coin Flipper</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
.coin {
font-size: 100px;
margin: 20px 0;
}
button {
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
#result {
font-size: 24px;
margin: 20px 0;
}
</style>
</head>
<body>
<h1>Coin Flipper</h1>
<div class="coin" id="coinDisplay">🪙</div>
<div id="result"></div>
<button onclick="flipCoin()">Flip Coin</button>
<script>
async function flipCoin() {
const response = await fetch('/flip');
const result = await response.json();
const resultDiv = document.getElementById('result');
const coinDisplay = document.getElementById('coinDisplay');
// Animate coin
coinDisplay.style.transform = 'rotateY(0deg)';
setTimeout(() => {
coinDisplay.style.transform = 'rotateY(1800deg)';
coinDisplay.style.transition = 'transform 1s ease-out';
setTimeout(() => {
resultDiv.textContent = result.outcome;
coinDisplay.textContent = result.outcome === 'Heads' ? '🪙' : '💿';
}, 1000);
}, 50);
}
</script>
</body>
</html>
'''
@app.get('/flip')
async def flip():
result = 'Heads' if random.random() < 0.5 else 'Tails'
return {'outcome': result}
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)