Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/sgis/maps/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
clipmap functions from the 'maps' module.
"""
import warnings
import tempfile
from statistics import mean

import branca as bc
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 10 additions & 6 deletions src/sgis/maps/httpserver.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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

Expand Down