|
1 | 1 | from flask import Flask, render_template_string |
| 2 | +import random |
2 | 3 |
|
3 | 4 | app = Flask(__name__) |
4 | 5 |
|
5 | | -# Simple attractive HTML page |
6 | 6 | HTML_PAGE = """ |
7 | 7 | <!DOCTYPE html> |
8 | 8 | <html lang="en"> |
9 | 9 | <head> |
10 | 10 | <meta charset="UTF-8"> |
11 | 11 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
12 | | - <title>Azure Flask Demo</title> |
| 12 | + <title>Rock Paper Scissors - Azure Demo</title> |
13 | 13 | <style> |
14 | 14 | body { |
15 | 15 | font-family: Arial, sans-serif; |
16 | | - background: linear-gradient(135deg, #4facfe, #00f2fe); |
| 16 | + background: linear-gradient(135deg, #667eea, #764ba2); |
17 | 17 | height: 100vh; |
18 | 18 | display: flex; |
19 | | - align-items: center; |
20 | 19 | justify-content: center; |
| 20 | + align-items: center; |
21 | 21 | margin: 0; |
| 22 | + color: #fff; |
22 | 23 | } |
23 | | - .card { |
24 | | - background: rgba(255,255,255,0.9); |
25 | | - padding: 30px 40px; |
26 | | - border-radius: 15px; |
27 | | - box-shadow: 0px 8px 20px rgba(0,0,0,0.2); |
| 24 | + .game { |
28 | 25 | text-align: center; |
29 | | - max-width: 400px; |
| 26 | + background: rgba(0,0,0,0.5); |
| 27 | + padding: 30px; |
| 28 | + border-radius: 15px; |
| 29 | + box-shadow: 0 8px 20px rgba(0,0,0,0.3); |
30 | 30 | } |
31 | 31 | h1 { |
32 | | - color: #0078D4; |
33 | | - margin-bottom: 15px; |
| 32 | + margin-bottom: 20px; |
34 | 33 | } |
35 | | - p { |
36 | | - font-size: 18px; |
37 | | - color: #444; |
38 | | - } |
39 | | - .btn { |
40 | | - display: inline-block; |
41 | | - margin-top: 20px; |
| 34 | + .choices button { |
| 35 | + margin: 10px; |
42 | 36 | padding: 10px 20px; |
43 | | - background: #0078D4; |
| 37 | + border: none; |
| 38 | + background: #ff9800; |
44 | 39 | color: white; |
45 | | - text-decoration: none; |
46 | 40 | border-radius: 8px; |
| 41 | + cursor: pointer; |
| 42 | + font-size: 16px; |
47 | 43 | transition: 0.3s; |
48 | 44 | } |
49 | | - .btn:hover { |
50 | | - background: #005a9e; |
| 45 | + .choices button:hover { |
| 46 | + background: #e68900; |
| 47 | + } |
| 48 | + #result { |
| 49 | + margin-top: 20px; |
| 50 | + font-size: 20px; |
| 51 | + font-weight: bold; |
51 | 52 | } |
52 | 53 | </style> |
53 | 54 | </head> |
54 | 55 | <body> |
55 | | - <div class="card"> |
56 | | - <h1>🚀 Azure Flask Demo</h1> |
57 | | - <p>This web app is deployed on <b>Azure App Service</b> using GitHub!</p> |
58 | | - <a class="btn" href="https://github.com" target="_blank">View on GitHub</a> |
| 56 | + <div class="game"> |
| 57 | + <h1>✊ ✋ ✌ Rock Paper Scissors</h1> |
| 58 | + <p>Choose one:</p> |
| 59 | + <div class="choices"> |
| 60 | + <button onclick="play('rock')">Rock</button> |
| 61 | + <button onclick="play('paper')">Paper</button> |
| 62 | + <button onclick="play('scissors')">Scissors</button> |
| 63 | + </div> |
| 64 | + <div id="result"></div> |
59 | 65 | </div> |
| 66 | +
|
| 67 | + <script> |
| 68 | + function play(choice) { |
| 69 | + fetch(`/play?choice=${choice}`) |
| 70 | + .then(response => response.json()) |
| 71 | + .then(data => { |
| 72 | + document.getElementById("result").innerHTML = |
| 73 | + "You chose: " + data.user + "<br>" + |
| 74 | + "Computer chose: " + data.computer + "<br><br>" + |
| 75 | + "<b>" + data.result + "</b>"; |
| 76 | + }); |
| 77 | + } |
| 78 | + </script> |
60 | 79 | </body> |
61 | 80 | </html> |
62 | 81 | """ |
|
65 | 84 | def home(): |
66 | 85 | return render_template_string(HTML_PAGE) |
67 | 86 |
|
| 87 | +@app.route("/play") |
| 88 | +def play(): |
| 89 | + from flask import request, jsonify |
| 90 | + user_choice = request.args.get("choice") |
| 91 | + choices = ["rock", "paper", "scissors"] |
| 92 | + comp_choice = random.choice(choices) |
| 93 | + |
| 94 | + if user_choice == comp_choice: |
| 95 | + result = "It's a Tie!" |
| 96 | + elif (user_choice == "rock" and comp_choice == "scissors") or \ |
| 97 | + (user_choice == "paper" and comp_choice == "rock") or \ |
| 98 | + (user_choice == "scissors" and comp_choice == "paper"): |
| 99 | + result = "🎉 You Win!" |
| 100 | + else: |
| 101 | + result = "😢 You Lose!" |
| 102 | + |
| 103 | + return jsonify({"user": user_choice, "computer": comp_choice, "result": result}) |
| 104 | + |
68 | 105 | if __name__ == "__main__": |
69 | 106 | app.run(host="0.0.0.0", port=8000) |
| 107 | + |
0 commit comments