From b21410d1b121d5405d1dca1885740727d47e7fb1 Mon Sep 17 00:00:00 2001 From: philippe Date: Thu, 23 Jan 2025 10:17:39 -0500 Subject: [PATCH 1/5] Fix run host port arguments --- dash/dash.py | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index ec243c43a0..2e460f13f7 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -2007,23 +2007,23 @@ def delete_resource(resources): def run( self, - host="127.0.0.1", - port="8050", - proxy=None, - debug=None, + host: Optional[str] = None, + port: Optional[str] = None, + proxy: Optional[str] = None, + debug: Optional[bool] = None, jupyter_mode: Optional[JupyterDisplayMode] = None, - jupyter_width="100%", - jupyter_height=650, - jupyter_server_url=None, - dev_tools_ui=None, - dev_tools_props_check=None, - dev_tools_serve_dev_bundles=None, - dev_tools_hot_reload=None, - dev_tools_hot_reload_interval=None, - dev_tools_hot_reload_watch_interval=None, - dev_tools_hot_reload_max_retry=None, - dev_tools_silence_routes_logging=None, - dev_tools_prune_errors=None, + jupyter_width: str = "100%", + jupyter_height: int = 650, + jupyter_server_url: Optional[str] = None, + dev_tools_ui: Optional[bool] = None, + dev_tools_props_check: Optional[bool] = None, + dev_tools_serve_dev_bundles: Optional[bool] = None, + dev_tools_hot_reload: Optional[bool] = None, + dev_tools_hot_reload_interval: Optional[int] = None, + dev_tools_hot_reload_watch_interval: Optional[int] = None, + dev_tools_hot_reload_max_retry: Optional[int] = None, + dev_tools_silence_routes_logging: Optional[bool] = None, + dev_tools_prune_errors: Optional[bool] = None, **flask_run_options, ): """Start the flask server in local mode, you should not run this on a @@ -2032,11 +2032,11 @@ def run( If a parameter can be set by an environment variable, that is listed too. Values provided here take precedence over environment variables. - :param host: Host IP used to serve the application + :param host: Host IP used to serve the application, default to "127.0.0.1" env: ``HOST`` :type host: string - :param port: Port used to serve the application + :param port: Port used to serve the application, default to "8050" env: ``PORT`` :type port: int @@ -2137,14 +2137,14 @@ def run( # Evaluate the env variables at runtime - host = host or os.getenv("HOST") - port = port or os.getenv("PORT") + host = host or os.getenv("HOST", "127.0.0.1") + port = port or os.getenv("PORT", "8050") proxy = proxy or os.getenv("DASH_PROXY") # Verify port value try: - port = int(port) - assert port in range(1, 65536) + server_port = int(port) + assert server_port in range(1, 65536) except Exception as e: e.args = (f"Expecting an integer from 1 to 65535, found port={repr(port)}",) raise @@ -2203,11 +2203,13 @@ def verify_url_part(served_part, url_part, part_name): width=jupyter_width, height=jupyter_height, host=host, - port=port, + port=server_port, server_url=jupyter_server_url, ) else: - self.server.run(host=host, port=port, debug=debug, **flask_run_options) + self.server.run( + host=host, port=server_port, debug=debug, **flask_run_options + ) def enable_pages(self): if not self.use_pages: From 64d540b427f2b478a2e96e3f0a5d38ea6a9bf897 Mon Sep 17 00:00:00 2001 From: philippe Date: Thu, 23 Jan 2025 10:54:55 -0500 Subject: [PATCH 2/5] Fix conda reading host from environment --- dash/dash.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dash/dash.py b/dash/dash.py index 2e460f13f7..9c429fcd2b 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -2005,6 +2005,7 @@ def delete_resource(resources): # pylint: disable=protected-access delete_resource(self.css._resources._resources) + # pylint: disable=too-many-branches def run( self, host: Optional[str] = None, @@ -2137,7 +2138,13 @@ def run( # Evaluate the env variables at runtime - host = host or os.getenv("HOST", "127.0.0.1") + if "CONDA_PREFIX" in os.environ: + # Some conda systems has issue with setting the host environment + # to an invalid hostname. + # Related issue: https://github.com/plotly/dash/issues/3069 + host = host or "127.0.0.1" + else: + host = host or os.getenv("HOST", "127.0.0.1") port = port or os.getenv("PORT", "8050") proxy = proxy or os.getenv("DASH_PROXY") From 7df84a46b0b40d56d705493eb8ef6e15d0daab2b Mon Sep 17 00:00:00 2001 From: philippe Date: Thu, 23 Jan 2025 11:03:38 -0500 Subject: [PATCH 3/5] Fix server port assertions --- dash/dash.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index 9c429fcd2b..a7fbf0edf1 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -2153,7 +2153,9 @@ def run( server_port = int(port) assert server_port in range(1, 65536) except Exception as e: - e.args = (f"Expecting an integer from 1 to 65535, found port={repr(port)}",) + e.args = ( + f"Expecting an integer from 1 to 65535, found port={repr(server_port)}", + ) raise # so we only see the "Running on" message once with hot reloading @@ -2179,7 +2181,7 @@ def verify_url_part(served_part, url_part, part_name): verify_url_part(served_url.scheme, protocol, "protocol") verify_url_part(served_url.hostname, host, "host") - verify_url_part(served_url.port, port, "port") + verify_url_part(served_url.port, server_port, "port") display_url = ( proxied_url.scheme, From 9ddbe6def2a593e3be3faaadb619b75b01835207 Mon Sep 17 00:00:00 2001 From: philippe Date: Thu, 23 Jan 2025 11:06:48 -0500 Subject: [PATCH 4/5] port to Union[str,int] --- dash/dash.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dash/dash.py b/dash/dash.py index a7fbf0edf1..5129dca78a 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -2009,7 +2009,7 @@ def delete_resource(resources): def run( self, host: Optional[str] = None, - port: Optional[str] = None, + port: Optional[Union[str, int]] = None, proxy: Optional[str] = None, debug: Optional[bool] = None, jupyter_mode: Optional[JupyterDisplayMode] = None, From 094087fddb8e22f61b7a1927588b94e7cd578813 Mon Sep 17 00:00:00 2001 From: philippe Date: Thu, 23 Jan 2025 11:32:45 -0500 Subject: [PATCH 5/5] fix ports --- dash/dash.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index 5129dca78a..d00a4bf2a5 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -2150,12 +2150,10 @@ def run( # Verify port value try: - server_port = int(port) - assert server_port in range(1, 65536) + port = int(port) + assert port in range(1, 65536) except Exception as e: - e.args = ( - f"Expecting an integer from 1 to 65535, found port={repr(server_port)}", - ) + e.args = (f"Expecting an integer from 1 to 65535, found port={repr(port)}",) raise # so we only see the "Running on" message once with hot reloading @@ -2181,7 +2179,7 @@ def verify_url_part(served_part, url_part, part_name): verify_url_part(served_url.scheme, protocol, "protocol") verify_url_part(served_url.hostname, host, "host") - verify_url_part(served_url.port, server_port, "port") + verify_url_part(served_url.port, port, "port") display_url = ( proxied_url.scheme, @@ -2212,13 +2210,11 @@ def verify_url_part(served_part, url_part, part_name): width=jupyter_width, height=jupyter_height, host=host, - port=server_port, + port=port, server_url=jupyter_server_url, ) else: - self.server.run( - host=host, port=server_port, debug=debug, **flask_run_options - ) + self.server.run(host=host, port=port, debug=debug, **flask_run_options) def enable_pages(self): if not self.use_pages: