Skip to content

Commit cc709ae

Browse files
committed
one Visualizer host (for fs+ws), load from window.location in browser
1 parent 5bf5092 commit cc709ae

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

pylabrobot/visualizer/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@
9797
<script src="./lib.js"></script>
9898
<script src="./vis.js"></script>
9999

100-
<input type="text" id="fs_host" value="{{ fs_host }}" hidden />
101100
<input type="text" id="fs_port" value="{{ fs_port }}" hidden />
102-
<input type="text" id="ws_host" value="{{ ws_host }}" hidden />
103101
<input type="text" id="ws_port" value="{{ ws_port }}" hidden />
104102
</body>
105103
</html>

pylabrobot/visualizer/vis.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ function openSocket() {
123123

124124
socketLoading = true;
125125
updateStatusLabel("loading");
126-
let wsHostInput = document.querySelector(`input[id="ws_host"]`);
127126
let wsPortInput = document.querySelector(`input[id="ws_port"]`);
128-
let wsHost = wsHostInput.value;
127+
let wsHost = window.location.hostname;
129128
let wsPort = wsPortInput.value;
130129
webSocket = new WebSocket(`ws://${wsHost}:${wsPort}/`);
131130

pylabrobot/visualizer/visualizer.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,17 @@ class Visualizer:
4242
def __init__(
4343
self,
4444
resource: Resource,
45-
ws_host: str = "127.0.0.1",
45+
host: str = "127.0.0.1",
4646
ws_port: int = 2121,
47-
fs_host: str = "127.0.0.1",
4847
fs_port: int = 1337,
4948
open_browser: bool = True,
5049
):
5150
"""Create a new Visualizer. Use :meth:`.setup` to start the visualization.
5251
5352
Args:
54-
ws_host: The hostname of the websocket server.
53+
host: The hostname of the file and websocket server.
5554
ws_port: The port of the websocket server. If this port is in use, the port will be
5655
incremented until a free port is found.
57-
fs_host: The hostname of the file server. This is where the visualization will be served.
5856
fs_port: The port of the file server. If this port is in use, the port will be incremented
5957
until a free port is found.
6058
open_browser: If `True`, the visualizer will open a browser window when it is started.
@@ -78,16 +76,16 @@ def register_state_update(resource):
7876

7977
register_state_update(resource)
8078

79+
self.host = host
80+
8181
# file server attributes
82-
self.fs_host = fs_host
8382
self.fs_port = fs_port
8483
self.open_browser = open_browser
8584

8685
self._httpd: Optional[http.server.HTTPServer] = None
8786
self._fst: Optional[threading.Thread] = None
8887

8988
# websocket server attributes
90-
self.ws_host = ws_host
9189
self.ws_port = ws_port
9290
self._id = 0
9391

@@ -286,10 +284,8 @@ async def run_server():
286284
self._stop_ = self.loop.create_future()
287285
while True:
288286
try:
289-
async with websockets.legacy.server.serve(
290-
self._socket_handler, self.ws_host, self.ws_port
291-
):
292-
print(f"Websocket server started at http://{self.ws_host}:{self.ws_port}")
287+
async with websockets.legacy.server.serve(self._socket_handler, self.host, self.ws_port):
288+
print(f"Websocket server started at http://{self.host}:{self.ws_port}")
293289
lock.release()
294290
await self.stop_
295291
break
@@ -323,10 +319,9 @@ def _run_file_server(self):
323319
)
324320

325321
def start_server(lock):
326-
ws_host, ws_port, fs_host, fs_port = (
327-
self.ws_host,
322+
host, ws_port, fs_port = (
323+
self.host,
328324
self.ws_port,
329-
self.fs_host,
330325
self.fs_port,
331326
)
332327

@@ -347,9 +342,7 @@ def do_GET(self) -> None:
347342
with open(os.path.join(path, "index.html"), "r", encoding="utf-8") as f:
348343
content = f.read()
349344

350-
content = content.replace("{{ ws_host }}", ws_host)
351345
content = content.replace("{{ ws_port }}", str(ws_port))
352-
content = content.replace("{{ fs_host }}", fs_host)
353346
content = content.replace("{{ fs_port }}", str(fs_port))
354347

355348
self.send_response(200)
@@ -362,11 +355,11 @@ def do_GET(self) -> None:
362355
while True:
363356
try:
364357
self._httpd = http.server.HTTPServer(
365-
(self.fs_host, self.fs_port),
358+
(self.host, self.fs_port),
366359
QuietSimpleHTTPRequestHandler,
367360
)
368361
print(
369-
f"File server started at http://{self.fs_host}:{self.fs_port} . "
362+
f"File server started at http://{self.host}:{self.fs_port} . "
370363
"Open this URL in your browser."
371364
)
372365
lock.release()
@@ -391,7 +384,7 @@ def do_GET(self) -> None:
391384
time.sleep(0.001)
392385

393386
if self.open_browser:
394-
webbrowser.open(f"http://{self.fs_host}:{self.fs_port}")
387+
webbrowser.open(f"http://{self.host}:{self.fs_port}")
395388

396389
async def stop(self):
397390
"""Stop the visualizer.

0 commit comments

Comments
 (0)