Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions taipy/gui/_gui_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _GuiCLI(_AbstractCLI):
"nargs": "?",
"default": "",
"const": "",
"help": "Specify client url",
"help": "Specify client URL",
},
("--ngrok-token",): {
"dest": "taipy_ngrok_token",
Expand Down Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we not use the same dest with action: store_false ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I chose to use the same pattern as for other options for homogeneity.
But I had the same question, with the same answer. This could be simplified.

"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 the dark mode to the GUI application",
"action": "store_true",
},
"--light-mode": {
"dest": "taipy_light_mode",
"help": "Apply the 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()
Expand All @@ -109,12 +143,20 @@ def create_run_parser(cls):
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()
Expand Down
6 changes: 6 additions & 0 deletions taipy/gui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ def _handle_argparse(self):
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:
Expand Down
24 changes: 24 additions & 0 deletions tests/gui/gui_specific/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading