_gui_cli.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from typing import Dict, Tuple
  12. from taipy.common._cli._base_cli._abstract_cli import _AbstractCLI
  13. from taipy.common._cli._base_cli._taipy_parser import _TaipyParser
  14. from ._hook import _Hooks
  15. class _GuiCLI(_AbstractCLI):
  16. """Command-line interface of GUI."""
  17. __GUI_ARGS: Dict[Tuple, Dict] = {
  18. ("--port", "-P"): {
  19. "dest": "taipy_port",
  20. "metavar": "PORT",
  21. "nargs": "?",
  22. "default": "",
  23. "const": "",
  24. "help": "Specify server port",
  25. },
  26. ("--host", "-H"): {
  27. "dest": "taipy_host",
  28. "metavar": "HOST",
  29. "nargs": "?",
  30. "default": "",
  31. "const": "",
  32. "help": "Specify server host",
  33. },
  34. ("--client-url",): {
  35. "dest": "taipy_client_url",
  36. "metavar": "CLIENT_URL",
  37. "nargs": "?",
  38. "default": "",
  39. "const": "",
  40. "help": "Specify client url",
  41. },
  42. ("--ngrok-token",): {
  43. "dest": "taipy_ngrok_token",
  44. "metavar": "NGROK_TOKEN",
  45. "nargs": "?",
  46. "default": "",
  47. "const": "",
  48. "help": "Specify NGROK Authtoken",
  49. },
  50. ("--webapp-path",): {
  51. "dest": "taipy_webapp_path",
  52. "metavar": "WEBAPP_PATH",
  53. "nargs": "?",
  54. "default": "",
  55. "const": "",
  56. "help": "The path to the web app to be used. The default is the webapp directory under gui in the Taipy GUI package directory.", # noqa: E501
  57. },
  58. ("--upload-folder",): {
  59. "dest": "taipy_upload_folder",
  60. "metavar": "UPLOAD_FOLDER",
  61. "nargs": "?",
  62. "default": "",
  63. "const": "",
  64. "help": "The path to the folder where uploaded files from Taipy GUI will be stored.",
  65. },
  66. }
  67. __DEBUG_ARGS: Dict[str, Dict] = {
  68. "--debug": {"dest": "taipy_debug", "help": "Turn on debug", "action": "store_true"},
  69. "--no-debug": {"dest": "taipy_no_debug", "help": "Turn off debug", "action": "store_true"},
  70. }
  71. __RELOADER_ARGS: Dict[str, Dict] = {
  72. "--use-reloader": {"dest": "taipy_use_reloader", "help": "Auto reload on code changes", "action": "store_true"},
  73. "--no-reloader": {"dest": "taipy_no_reloader", "help": "No reload on code changes", "action": "store_true"},
  74. }
  75. @classmethod
  76. def create_parser(cls):
  77. gui_parser = _TaipyParser._add_groupparser("Taipy GUI", "Optional arguments for Taipy GUI service")
  78. for args, arg_dict in cls.__GUI_ARGS.items():
  79. taipy_arg = (args[0], cls.__add_taipy_prefix(args[0]), *args[1:])
  80. gui_parser.add_argument(*taipy_arg, **arg_dict)
  81. debug_group = gui_parser.add_mutually_exclusive_group()
  82. for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
  83. debug_group.add_argument(debug_arg, cls.__add_taipy_prefix(debug_arg), **debug_arg_dict)
  84. reloader_group = gui_parser.add_mutually_exclusive_group()
  85. for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
  86. reloader_group.add_argument(reloader_arg, cls.__add_taipy_prefix(reloader_arg), **reloader_arg_dict)
  87. if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
  88. hook_group = gui_parser.add_mutually_exclusive_group()
  89. for hook_arg, hook_arg_dict in hook_cli_arg.items():
  90. hook_group.add_argument(hook_arg, cls.__add_taipy_prefix(hook_arg), **hook_arg_dict)
  91. @classmethod
  92. def create_run_parser(cls):
  93. run_parser = _TaipyParser._add_subparser("run", help="Run a Taipy application.")
  94. for args, arg_dict in cls.__GUI_ARGS.items():
  95. run_parser.add_argument(*args, **arg_dict)
  96. debug_group = run_parser.add_mutually_exclusive_group()
  97. for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
  98. debug_group.add_argument(debug_arg, **debug_arg_dict)
  99. reloader_group = run_parser.add_mutually_exclusive_group()
  100. for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
  101. reloader_group.add_argument(reloader_arg, **reloader_arg_dict)
  102. if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
  103. hook_group = run_parser.add_mutually_exclusive_group()
  104. for hook_arg, hook_arg_dict in hook_cli_arg.items():
  105. hook_group.add_argument(hook_arg, **hook_arg_dict)
  106. @classmethod
  107. def handle_command(cls):
  108. args, _ = _TaipyParser._parser.parse_known_args()
  109. return args
  110. @classmethod
  111. def __add_taipy_prefix(cls, key: str):
  112. if key.startswith("--no-"):
  113. return key[:5] + "taipy-" + key[5:]
  114. return key[:2] + "taipy-" + key[2:]