浏览代码

Merge branch 'develop' into fix/remove-C405-rule

ooo oo 1 年之前
父节点
当前提交
876adc8764
共有 3 个文件被更改,包括 14 次插入4 次删除
  1. 1 0
      .github/workflows/overall-tests.yml
  2. 10 3
      taipy/gui/config.py
  3. 3 1
      taipy/gui/server.py

+ 1 - 0
.github/workflows/overall-tests.yml

@@ -13,6 +13,7 @@ jobs:
 
   coverage:
     runs-on: ubuntu-latest
+    if: ${{ github.event_name == 'pull_request' }}
     steps:
       - uses: actions/checkout@v4
 

+ 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")