From 1522f7ea26da654180df8540f3ee799d629c8253 Mon Sep 17 00:00:00 2001 From: jayce Date: Sun, 19 Jun 2022 19:23:00 -0400 Subject: [PATCH 1/3] mimic dash error if running app on port already in use --- plotly_resampler/figure_resampler/figure_resampler.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plotly_resampler/figure_resampler/figure_resampler.py b/plotly_resampler/figure_resampler/figure_resampler.py index efccca25..ade2e27b 100644 --- a/plotly_resampler/figure_resampler/figure_resampler.py +++ b/plotly_resampler/figure_resampler/figure_resampler.py @@ -10,6 +10,8 @@ __author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost" +import sys +import socket import warnings from typing import Tuple @@ -160,6 +162,15 @@ def show_dash( self._host = kwargs.get("host", "127.0.0.1") self._port = kwargs.get("port", "8050") + # check if self._port is already in use + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + if s.connect_ex(('localhost', int(self._port))) == 0: + error_message = "Address already in use\n" \ + f"Port {self._port} is in use by another program. " \ + "Either identify and stop that program, or start the server with a different port." + print(error_message) + sys.exit(1) + app.run_server(mode=mode, **kwargs) def stop_server(self, warn: bool = True): From ea4c3aa86121ce5d864cac13a6a95b0012a5e50c Mon Sep 17 00:00:00 2001 From: jayce Date: Mon, 20 Jun 2022 08:11:44 -0400 Subject: [PATCH 2/3] localhost -> self._host --- plotly_resampler/figure_resampler/figure_resampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly_resampler/figure_resampler/figure_resampler.py b/plotly_resampler/figure_resampler/figure_resampler.py index ade2e27b..fea68e9c 100644 --- a/plotly_resampler/figure_resampler/figure_resampler.py +++ b/plotly_resampler/figure_resampler/figure_resampler.py @@ -164,7 +164,7 @@ def show_dash( # check if self._port is already in use with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - if s.connect_ex(('localhost', int(self._port))) == 0: + if s.connect_ex((self._host, int(self._port))) == 0: error_message = "Address already in use\n" \ f"Port {self._port} is in use by another program. " \ "Either identify and stop that program, or start the server with a different port." From 0d788d1351b688c4995831e55d373bfab672160e Mon Sep 17 00:00:00 2001 From: jayce Date: Wed, 22 Jun 2022 20:35:58 -0400 Subject: [PATCH 3/3] raise error instead of exit --- plotly_resampler/figure_resampler/figure_resampler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plotly_resampler/figure_resampler/figure_resampler.py b/plotly_resampler/figure_resampler/figure_resampler.py index fea68e9c..2d7cdfaa 100644 --- a/plotly_resampler/figure_resampler/figure_resampler.py +++ b/plotly_resampler/figure_resampler/figure_resampler.py @@ -168,8 +168,7 @@ def show_dash( error_message = "Address already in use\n" \ f"Port {self._port} is in use by another program. " \ "Either identify and stop that program, or start the server with a different port." - print(error_message) - sys.exit(1) + raise socket.error(error_message) app.run_server(mode=mode, **kwargs)