瀏覽代碼

Allow auto port assignment (#687) (#966)

* Allow auto port

* Per FJ

* Per fabien FJ

---------

Co-authored-by: Fred Lefévère-Laoide <90181748+FredLL-Avaiga@users.noreply.github.com>
Dinh Long Nguyen 1 年之前
父節點
當前提交
6713e4d31d
共有 2 個文件被更改,包括 13 次插入4 次删除
  1. 10 3
      taipy/gui/config.py
  2. 3 1
      taipy/gui/server.py

+ 10 - 3
taipy/gui/config.py

@@ -133,7 +133,7 @@ Config = t.TypedDict(
         "use_reloader": bool,
         "watermark": t.Optional[str],
         "webapp_path": t.Optional[str],
-        "port": int,
+        "port": t.Union[t.Literal["auto"], int],
     },
     total=False,
 )
@@ -189,7 +189,9 @@ class _Config(object):
 
         config = self.config
         if args.taipy_port:
-            if not _Config.__RE_PORT_NUMBER.match(args.taipy_port):
+            if str(args.taipy_port).strip() == "auto":
+                config["port"] = "auto"
+            elif not _Config.__RE_PORT_NUMBER.match(args.taipy_port):
                 _warn("Port value for --port option is not valid.")
             else:
                 config["port"] = int(args.taipy_port)
@@ -226,6 +228,8 @@ class _Config(object):
                 try:
                     if isinstance(value, dict) and isinstance(config[key], dict):
                         config[key].update(value)
+                    elif key == "port" and str(value).strip() == "auto":
+                        config["port"] = "auto"
                     else:
                         config[key] = value if config[key] is None else type(config[key])(value)  # type: ignore
                 except Exception as e:
@@ -239,7 +243,10 @@ class _Config(object):
                 key = key.lower()
                 if value is not None and key in config:
                     try:
-                        config[key] = value if config[key] is None else type(config[key])(value)  # type: ignore
+                        if key == "port" and str(value).strip() == "auto":
+                            config["port"] = "auto"
+                        else:
+                            config[key] = value if config[key] is None else type(config[key])(value)  # type: ignore
                     except Exception as e:
                         _warn(
                             f"Invalid env value in Gui.run(): {key} - {value}. Unable to parse value to the correct type",  # noqa: E501

+ 3 - 1
taipy/gui/server.py

@@ -268,6 +268,8 @@ class _Server:
     def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_unsafe_werkzeug, notebook_proxy):
         host_value = host if host != "0.0.0.0" else "localhost"
         self._host = host
+        if port == "auto":
+            port = self._get_random_port()
         self._port = port
         if _is_in_notebook() and notebook_proxy:  # pragma: no cover
             from .utils.proxy import NotebookProxy
@@ -281,7 +283,7 @@ class _Server:
             runtime_manager.add_gui(self._gui, port)
         if debug and not is_running_from_reloader() and _is_port_open(host_value, port):
             raise ConnectionError(
-                f"Port {port} is already opened on {host_value}. You have another server application running on the same port."  # noqa: E501
+                "Port {port} is already opened on {host} because another application is running on the same port. Please pick another port number and rerun with the 'port=<new_port>' option. You can also let Taipy choose a port number for you by running with the 'port=\"auto\"' option."  # noqa: E501
             )
         if not flask_log:
             log = logging.getLogger("werkzeug")