Spin up a simple HTTP server in a background thread to serve files. This is especially handy for quick demos or visualization purposes. Returns a shutdown() function that can be called to stop the server. :param host: Host/IP to bind to. Defaults to '0.0.0.0'. :param port: Por
(
host="0.0.0.0", port=8001, handler_class=http.server.SimpleHTTPRequestHandler
)
| 320 | |
| 321 | |
| 322 | def start_visualization_server( |
| 323 | host="0.0.0.0", port=8001, handler_class=http.server.SimpleHTTPRequestHandler |
| 324 | ): |
| 325 | """ |
| 326 | Spin up a simple HTTP server in a background thread to serve files. |
| 327 | This is especially handy for quick demos or visualization purposes. |
| 328 | |
| 329 | Returns a shutdown() function that can be called to stop the server. |
| 330 | |
| 331 | :param host: Host/IP to bind to. Defaults to '0.0.0.0'. |
| 332 | :param port: Port to listen on. Defaults to 8001. |
| 333 | :param handler_class: A handler class, defaults to SimpleHTTPRequestHandler. |
| 334 | :return: A no-argument function `shutdown` which, when called, stops the server. |
| 335 | """ |
| 336 | # Create the server |
| 337 | server = socketserver.TCPServer((host, port), handler_class) |
| 338 | |
| 339 | def _serve_forever(): |
| 340 | print(f"Visualization server running at: http://{host}:{port}") |
| 341 | server.serve_forever() |
| 342 | |
| 343 | # Start the server in a background thread |
| 344 | thread = Thread(target=_serve_forever, daemon=True) |
| 345 | thread.start() |
| 346 | |
| 347 | def shutdown(): |
| 348 | """ |
| 349 | Shuts down the server and blocks until the thread is joined. |
| 350 | """ |
| 351 | server.shutdown() # Signals the serve_forever() loop to stop |
| 352 | server.server_close() # Frees up the socket |
| 353 | thread.join() |
| 354 | print(f"Visualization server on port {port} has been shut down.") |
| 355 | |
| 356 | # Return only the shutdown function (the server runs in the background) |
| 357 | return shutdown |
no test coverage detected