Pārlūkot izejas kodu

Backport/fd page reload cookie removed (#2205)

* frontend decouple reload page on cookies removed (#2199)

* Better client_url handling (#2188)

* Better client_url handling

* fix tests

---------

Co-authored-by: Fred Lefévère-Laoide <90181748+FredLL-Avaiga@users.noreply.github.com>

---------

Co-authored-by: Fred Lefévère-Laoide <90181748+FredLL-Avaiga@users.noreply.github.com>
Dinh Long Nguyen 6 mēneši atpakaļ
vecāks
revīzija
d0e247d712

+ 1 - 1
taipy/gui/_default_config.py

@@ -46,7 +46,7 @@ default_config: Config = {
     "change_delay": None,
     "chart_dark_template": None,
     "base_url": "/",
-    "client_url": "http://localhost:{port}",
+    "client_url": None,
     "dark_mode": True,
     "dark_theme": None,
     "debug": False,

+ 1 - 1
taipy/gui/config.py

@@ -108,7 +108,7 @@ Config = t.TypedDict(
         "change_delay": t.Optional[int],
         "chart_dark_template": t.Optional[t.Dict[str, t.Any]],
         "base_url": t.Optional[str],
-        "client_url": str,
+        "client_url": t.Optional[str],
         "dark_mode": bool,
         "dark_theme": t.Optional[t.Dict[str, t.Any]],
         "data_url_max_size": t.Optional[int],

+ 10 - 8
taipy/gui/server.py

@@ -30,6 +30,7 @@ from flask import (
     jsonify,
     make_response,
     render_template,
+    render_template_string,
     request,
     send_from_directory,
 )
@@ -169,9 +170,8 @@ class _Server:
             if resource_handler_id is not None:
                 resource_handler = _ExternalResourceHandlerManager().get(resource_handler_id)
                 if resource_handler is None:
-                    response = make_response(
-                        "Cookie was deleted due to invalid resource handler id. Please restart the page manually.", 400
-                    )
+                    reload_html = "<html><head><style>body {background-color: black; margin: 0;}</style></head><body><script>location.reload();</script></body></html>"  # noqa: E501
+                    response = make_response(render_template_string(reload_html), 400)
                     response.set_cookie(
                         _Server._RESOURCE_HANDLER_ARG, "", secure=request.is_secure, httponly=True, expires=0, path="/"
                     )
@@ -317,8 +317,8 @@ class _Server:
         self._host = host
         if port == "auto":
             port = self._get_random_port(port_auto_ranges)
+        server_url = f"http://{host_value}:{port}"
         self._port = port
-        client_url = client_url.format(port=port)
         if _is_in_notebook() and notebook_proxy:  # pragma: no cover
             from .utils.proxy import NotebookProxy
 
@@ -337,12 +337,14 @@ class _Server:
             log = logging.getLogger("werkzeug")
             log.disabled = True
             if not is_running_from_reloader():
-                _TaipyLogger._get_logger().info(f" * Server starting on http://{host_value}:{port}")
+                _TaipyLogger._get_logger().info(f" * Server starting on {server_url}")
             else:
-                _TaipyLogger._get_logger().info(f" * Server reloaded on http://{host_value}:{port}")
-            _TaipyLogger._get_logger().info(f" * Application is accessible at {client_url}")
+                _TaipyLogger._get_logger().info(f" * Server reloaded on {server_url}")
+            if client_url is not None:
+                client_url = client_url.format(port=port)
+                _TaipyLogger._get_logger().info(f" * Application is accessible at {client_url}")
         if not is_running_from_reloader() and self._gui._get_config("run_browser", False):
-            webbrowser.open(client_url, new=2)
+            webbrowser.open(client_url or server_url, new=2)
         if _is_in_notebook() or run_in_thread:
             self._thread = KThread(target=self._run_notebook)
             self._thread.start()

+ 134 - 0
tests/gui/gui_specific/test_cli.py

@@ -0,0 +1,134 @@
+# Copyright 2021-2024 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+
+from unittest.mock import patch
+
+from taipy.gui import Gui
+
+
+def test_cli_port(gui: Gui):
+    with patch("sys.argv", ["prog"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("port") == 5000
+
+
+def test_cli_port_1(gui: Gui):
+    with patch("sys.argv", ["prog", "--port", "8080"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("port") == 8080
+
+
+def test_cli_port_2(gui: Gui):
+    with patch("sys.argv", ["prog", "-P", "9000"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("port") == 9000
+
+
+def test_cli_port_auto(gui: Gui):
+    with patch("sys.argv", ["prog", "--port", "auto"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("port") == "auto"
+
+
+def test_cli_host(gui: Gui):
+    with patch("sys.argv", ["prog"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("host") == "127.0.0.1"
+
+
+def test_cli_host_1(gui: Gui):
+    with patch("sys.argv", ["prog", "--host", "localhost"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("host") == "localhost"
+
+
+def test_cli_host_2(gui: Gui):
+    with patch("sys.argv", ["prog", "-H", "localhost"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("host") == "localhost"
+
+
+def test_taipy_debug(gui: Gui):
+    with patch("sys.argv", ["prog", "--debug"]):
+        gui.run(run_server=False, debug=False)
+        assert gui._config.config.get("debug") is True
+
+
+def test_taipy_no_debug(gui: Gui):
+    with patch("sys.argv", ["prog", "--no-debug"]):
+        gui.run(run_server=False, debug=True)
+        assert gui._config.config.get("debug") is False
+
+
+def test_taipy_reload(gui: Gui):
+    with patch("sys.argv", ["prog", "--use-reloader"]):
+        gui.run(run_server=False, use_reloader=False)
+        assert gui._config.config.get("use_reloader") is True
+
+
+def test_taipy_no_reload(gui: Gui):
+    with patch("sys.argv", ["prog", "--no-reloader"]):
+        gui.run(run_server=False, use_reloader=True)
+        assert gui._config.config.get("use_reloader") is False
+
+
+def test_taipy_run_browser(gui: Gui):
+    with patch("sys.argv", ["prog", "--run-browser"]):
+        gui.run(run_server=False, use_reloader=False)
+        assert gui._config.config.get("run_browser") is True
+
+
+def test_taipy_no_run_browser(gui: Gui):
+    with patch("sys.argv", ["prog", "--no-run-browser"]):
+        gui.run(run_server=False, use_reloader=True)
+        assert gui._config.config.get("run_browser") is False
+
+
+def test_taipy_dark_mode(gui: Gui):
+    with patch("sys.argv", ["prog", "--dark-mode"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("dark_mode") is True
+
+
+def test_taipy_light_mode(gui: Gui):
+    with patch("sys.argv", ["prog", "--light-mode"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("dark_mode") is False
+
+
+def test_ngrok_token(gui: Gui):
+    with patch("sys.argv", ["prog", "--ngrok-token", "token"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("ngrok_token") == "token"
+
+
+def test_webapp_path(gui: Gui):
+    with patch("sys.argv", ["prog", "--webapp-path", "path"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("webapp_path") == "path"
+
+
+def test_upload_folder(gui: Gui):
+    with patch("sys.argv", ["prog", "--upload-folder", "folder"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("upload_folder") == "folder"
+
+
+def test_client_url(gui: Gui):
+    with patch("sys.argv", ["prog"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("client_url") is None
+
+
+def test_client_url_1(gui: Gui):
+    with patch("sys.argv", ["prog", "--client-url", "url"]):
+        gui.run(run_server=False)
+        assert gui._config.config.get("client_url") == "url"