askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
counter_demo.py · 4.2 KB · 140 lines
| 1 | """ |
| 2 | Counter Demo Page |
| 3 | |
| 4 | Demonstrates the image counter with live updates. |
| 5 | """ |
| 6 | |
| 7 | |
| 8 | def GET(request): |
| 9 | """Return HTML demo page with counter image""" |
| 10 | |
| 11 | html = """ |
| 12 | <!DOCTYPE html> |
| 13 | <html> |
| 14 | <head> |
| 15 | <title>Image Counter Demo</title> |
| 16 | <style> |
| 17 | body { |
| 18 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif; |
| 19 | background: #0a0a0a; |
| 20 | color: #e0e0e0; |
| 21 | padding: 40px; |
| 22 | margin: 0; |
| 23 | display: flex; |
| 24 | flex-direction: column; |
| 25 | align-items: center; |
| 26 | justify-content: center; |
| 27 | min-height: 100vh; |
| 28 | } |
| 29 | h1 { |
| 30 | color: #fff; |
| 31 | margin-bottom: 20px; |
| 32 | } |
| 33 | .demo-box { |
| 34 | background: #1a1a1a; |
| 35 | padding: 40px; |
| 36 | border-radius: 12px; |
| 37 | border: 1px solid #333; |
| 38 | text-align: center; |
| 39 | max-width: 600px; |
| 40 | } |
| 41 | .counter-container { |
| 42 | margin: 30px 0; |
| 43 | padding: 20px; |
| 44 | background: #0a0a0a; |
| 45 | border-radius: 8px; |
| 46 | } |
| 47 | img { |
| 48 | display: block; |
| 49 | margin: 0 auto; |
| 50 | border: 2px solid #0ea5e9; |
| 51 | border-radius: 6px; |
| 52 | } |
| 53 | .info { |
| 54 | color: #999; |
| 55 | margin-top: 20px; |
| 56 | font-size: 14px; |
| 57 | line-height: 1.6; |
| 58 | } |
| 59 | button { |
| 60 | background: #0ea5e9; |
| 61 | color: white; |
| 62 | border: none; |
| 63 | padding: 12px 24px; |
| 64 | border-radius: 6px; |
| 65 | cursor: pointer; |
| 66 | font-size: 16px; |
| 67 | font-weight: 500; |
| 68 | margin: 10px; |
| 69 | } |
| 70 | button:hover { |
| 71 | background: #0284c7; |
| 72 | } |
| 73 | .url-box { |
| 74 | background: #0a0a0a; |
| 75 | padding: 15px; |
| 76 | border-radius: 6px; |
| 77 | margin-top: 20px; |
| 78 | font-family: monospace; |
| 79 | font-size: 13px; |
| 80 | color: #0ea5e9; |
| 81 | word-break: break-all; |
| 82 | } |
| 83 | </style> |
| 84 | </head> |
| 85 | <body> |
| 86 | <div class="demo-box"> |
| 87 | <h1>📊 Image Counter Demo</h1> |
| 88 | |
| 89 | <div class="counter-container"> |
| 90 | <img id="counter" src="/objects/basics_counter_image@station1" alt="Hit Counter"> |
| 91 | </div> |
| 92 | |
| 93 | <button onclick="reloadCounter()">🔄 Reload (Increment)</button> |
| 94 | <button onclick="resetCounter()">🔁 Reset Counter</button> |
| 95 | |
| 96 | <div class="info"> |
| 97 | <p><strong>How it works:</strong></p> |
| 98 | <p>Every time the image is loaded, the counter increments.</p> |
| 99 | <p>The count is stored in the object's state and persisted to disk.</p> |
| 100 | <p>The image is generated on-the-fly using PIL (Python Imaging Library).</p> |
| 101 | </div> |
| 102 | |
| 103 | <div class="url-box"> |
| 104 | <img src="http://localhost:8001/objects/basics_counter_image"> |
| 105 | </div> |
| 106 | </div> |
| 107 | |
| 108 | <script> |
| 109 | function reloadCounter() { |
| 110 | const img = document.getElementById('counter'); |
| 111 | // Add timestamp to bypass cache |
| 112 | img.src = '/objects/basics_counter_image@station1?t=' + new Date().getTime(); |
| 113 | } |
| 114 | |
| 115 | function resetCounter() { |
| 116 | fetch('/objects/basics_counter_image@station1', { |
| 117 | method: 'POST', |
| 118 | headers: {'Content-Type': 'application/json'}, |
| 119 | body: JSON.stringify({action: 'reset'}) |
| 120 | }) |
| 121 | .then(response => response.json()) |
| 122 | .then(data => { |
| 123 | alert(data.message); |
| 124 | reloadCounter(); |
| 125 | }) |
| 126 | .catch(err => alert('Error: ' + err)); |
| 127 | } |
| 128 | |
| 129 | // Auto-refresh every 5 seconds |
| 130 | setInterval(reloadCounter, 5000); |
| 131 | </script> |
| 132 | </body> |
| 133 | </html> |
| 134 | """ |
| 135 | |
| 136 | return { |
| 137 | 'status': 'ok', |
| 138 | 'content_type': 'text/html', |
| 139 | 'body': html |
| 140 | } |