Skip to content

Instantly share code, notes, and snippets.

@arivictor
Created October 24, 2025 04:57
Show Gist options
  • Select an option

  • Save arivictor/6174874e2cea630441016fca9ecd7418 to your computer and use it in GitHub Desktop.

Select an option

Save arivictor/6174874e2cea630441016fca9ecd7418 to your computer and use it in GitHub Desktop.
simple html app demo
from http.server import BaseHTTPRequestHandler, HTTPServer
HOST, PORT = "127.0.0.1", 8000
HTML_PAGE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTMX Example</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.min.js"></script>
</head>
<body>
<!-- have a button POST a click via AJAX -->
<button hx-post="/clicked" hx-swap="outerHTML">
Click Me
</button>
</body>
</html>
"""
count = 0
class SimpleHandler(BaseHTTPRequestHandler):
def _set_headers(self, status=200, content_type="text/html"):
self.send_response(status)
self.send_header("Content-type", content_type)
self.end_headers()
def do_GET(self):
if self.path == "/":
self._set_headers()
self.wfile.write(HTML_PAGE.encode())
else:
self._set_headers(404)
self.wfile.write(b"Not Found")
def do_POST(self):
if self.path == "/clicked":
self._set_headers()
global count
count += 1
self.wfile.write(f'<button hx-post="/clicked" hx-swap="outerHTML">Clicked {count} times!</button>'.encode())
else:
self._set_headers(404)
self.wfile.write(b"Not Found")
def run():
server = HTTPServer((HOST, PORT), SimpleHandler)
print(f"Server running on http://{HOST}:{PORT}")
server.serve_forever()
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment