1
0
Эх сурвалжийг харах

Add command line options for run_browser and dark_mode Gui.run() parameters (#2128)

Add the --run-browser, --no-run-browser, --dark-mode, and --light-mode CLI options.
Fabien Lelaquais 6 сар өмнө
parent
commit
c92b73190a

+ 53 - 11
taipy/gui/_gui_cli.py

@@ -43,7 +43,7 @@ class _GuiCLI(_AbstractCLI):
             "nargs": "?",
             "default": "",
             "const": "",
-            "help": "Specify client url",
+            "help": "Specify client URL",
         },
         ("--ngrok-token",): {
             "dest": "taipy_ngrok_token",
@@ -81,21 +81,55 @@ class _GuiCLI(_AbstractCLI):
         "--no-reloader": {"dest": "taipy_no_reloader", "help": "No reload on code changes", "action": "store_true"},
     }
 
+    __BROWSER_ARGS: Dict[str, Dict] = {
+        "--run-browser": {
+            "dest": "taipy_run_browser",
+            "help": "Open a new tab in the system browser",
+            "action": "store_true",
+        },
+        "--no-run-browser": {
+            "dest": "taipy_no_run_browser",
+            "help": "Don't open a new tab for the application",
+            "action": "store_true",
+        },
+    }
+
+    __DARK_LIGHT_MODE_ARGS: Dict[str, Dict] = {
+        "--dark-mode": {
+            "dest": "taipy_dark_mode",
+            "help": "Apply dark mode to the GUI application",
+            "action": "store_true",
+        },
+        "--light-mode": {
+            "dest": "taipy_light_mode",
+            "help": "Apply light mode to the GUI application",
+            "action": "store_true",
+        },
+    }
+
     @classmethod
     def create_parser(cls):
         gui_parser = _TaipyParser._add_groupparser("Taipy GUI", "Optional arguments for Taipy GUI service")
 
         for args, arg_dict in cls.__GUI_ARGS.items():
-            taipy_arg = (args[0], cls.__add_taipy_prefix(args[0]), *args[1:])
-            gui_parser.add_argument(*taipy_arg, **arg_dict)
+            arg = (args[0], cls.__add_taipy_prefix(args[0]), *args[1:])
+            gui_parser.add_argument(*arg, **arg_dict)
 
         debug_group = gui_parser.add_mutually_exclusive_group()
-        for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
-            debug_group.add_argument(debug_arg, cls.__add_taipy_prefix(debug_arg), **debug_arg_dict)
+        for arg, arg_dict in cls.__DEBUG_ARGS.items():
+            debug_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
 
         reloader_group = gui_parser.add_mutually_exclusive_group()
-        for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
-            reloader_group.add_argument(reloader_arg, cls.__add_taipy_prefix(reloader_arg), **reloader_arg_dict)
+        for arg, arg_dict in cls.__RELOADER_ARGS.items():
+            reloader_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
+
+        browser_group = gui_parser.add_mutually_exclusive_group()
+        for arg, arg_dict in cls.__BROWSER_ARGS.items():
+            browser_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
+
+        dark_light_mode_group = gui_parser.add_mutually_exclusive_group()
+        for arg, arg_dict in cls.__DARK_LIGHT_MODE_ARGS.items():
+            dark_light_mode_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
 
         if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
             hook_group = gui_parser.add_mutually_exclusive_group()
@@ -109,12 +143,20 @@ class _GuiCLI(_AbstractCLI):
             run_parser.add_argument(*args, **arg_dict)
 
         debug_group = run_parser.add_mutually_exclusive_group()
-        for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
-            debug_group.add_argument(debug_arg, **debug_arg_dict)
+        for arg, arg_dict in cls.__DEBUG_ARGS.items():
+            debug_group.add_argument(arg, **arg_dict)
 
         reloader_group = run_parser.add_mutually_exclusive_group()
-        for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
-            reloader_group.add_argument(reloader_arg, **reloader_arg_dict)
+        for arg, arg_dict in cls.__RELOADER_ARGS.items():
+            reloader_group.add_argument(arg, **arg_dict)
+
+        browser_group = run_parser.add_mutually_exclusive_group()
+        for arg, arg_dict in cls.__BROWSER_ARGS.items():
+            browser_group.add_argument(arg, **arg_dict)
+
+        dark_light_mode_group = run_parser.add_mutually_exclusive_group()
+        for arg, arg_dict in cls.__DARK_LIGHT_MODE_ARGS.items():
+            dark_light_mode_group.add_argument(arg, **arg_dict)
 
         if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
             hook_group = run_parser.add_mutually_exclusive_group()

+ 7 - 1
taipy/gui/config.py

@@ -214,6 +214,12 @@ class _Config(object):
             config["use_reloader"] = True
         if args.taipy_no_reloader:
             config["use_reloader"] = False
+        if args.taipy_run_browser:
+            config["run_browser"] = True
+        if args.taipy_no_run_browser:
+            config["run_browser"] = False
+        if args.taipy_dark_mode or args.taipy_light_mode:
+            config["dark_mode"] = not args.taipy_light_mode
         if args.taipy_ngrok_token:
             config["ngrok_token"] = args.taipy_ngrok_token
         if args.taipy_webapp_path:
@@ -250,7 +256,7 @@ class _Config(object):
                         config[key] = value if config.get(key) is None else type(config.get(key))(value)  # type: ignore[reportCallIssue]
                 except Exception as e:
                     _warn(
-                        f"Invalid keyword arguments value in Gui.run {key} - {value}. Unable to parse value to the correct type",  # noqa: E501
+                        f"Invalid keyword arguments value in Gui.run(): {key} - {value}. Unable to parse value to the correct type",  # noqa: E501
                         e,
                     )
         # Load config from env file

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

@@ -80,6 +80,30 @@ def test_taipy_no_reload(gui: Gui):
         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)