diff --git a/src/sgis/maps/explore.py b/src/sgis/maps/explore.py index 71e0fe86..6e467850 100644 --- a/src/sgis/maps/explore.py +++ b/src/sgis/maps/explore.py @@ -4,6 +4,7 @@ clipmap functions from the 'maps' module. """ import warnings +import tempfile from statistics import mean import branca as bc @@ -197,7 +198,9 @@ def _explore(self, **kwargs): self._create_continous_map() if self.browser: - run_html_server(self.map._repr_html_()) + _, file_name = tempfile.mkstemp(suffix='.html') + self.map.save(file_name) + run_html_server(file_name) else: display(self.map) diff --git a/src/sgis/maps/httpserver.py b/src/sgis/maps/httpserver.py index 2379b5af..03c009ac 100644 --- a/src/sgis/maps/httpserver.py +++ b/src/sgis/maps/httpserver.py @@ -1,11 +1,12 @@ import os import webbrowser +import shutil from http.server import BaseHTTPRequestHandler, HTTPServer from IPython.core.display import HTML, display -def run_html_server(contents: str | None = None, port: int = 3000): +def run_html_server(contents_path: str | None = None, port: int = 3000): """ Run a simple, temporary http web server for serving static HTML content. """ @@ -31,17 +32,20 @@ class HTTPServerRequestHandler(BaseHTTPRequestHandler): """ A handler of request for the server, hosting static content. """ - def do_GET(self): """Handle GET requests.""" self.send_response(200) - self.send_header("Content-type", "text/html") + fileSize = os.path.getsize(contents_path) + self.send_header("Content-Length", str(fileSize)) + self.send_header('Content-type', 'text/html') self.end_headers() - if self.path == "/stop": - self.wfile.write(bytes("The server is stopped", encoding="utf-8")) + if self.path == '/stop': + self.wfile.write(bytes('The server is stopped', encoding='utf-8')) + os.remove(contents_path) raise KeyboardInterrupt else: - self.wfile.write(bytes(contents, encoding="utf-8")) + with open(contents_path, 'rb') as contents: + shutil.copyfileobj(contents, self.wfile) HTTPServerRequestHandler.allow_reuse_address = True