_gui_cli.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. __BROWSER_ARGS: Dict[str, Dict] = {
  76. "--run-browser": {
  77. "dest": "taipy_run_browser",
  78. "help": "Open a new tab in the system browser",
  79. "action": "store_true",
  80. },
  81. "--no-run-browser": {
  82. "dest": "taipy_no_run_browser",
  83. "help": "Don't open a new tab for the application",
  84. "action": "store_true",
  85. },
  86. }
  87. __DARK_LIGHT_MODE_ARGS: Dict[str, Dict] = {
  88. "--dark-mode": {
  89. "dest": "taipy_dark_mode",
  90. "help": "Apply dark mode to the GUI application",
  91. "action": "store_true",
  92. },
  93. "--light-mode": {
  94. "dest": "taipy_light_mode",
  95. "help": "Apply light mode to the GUI application",
  96. "action": "store_true",
  97. },
  98. }
  99. @classmethod
  100. def create_parser(cls):
  101. gui_parser = _TaipyParser._add_groupparser("Taipy GUI", "Optional arguments for Taipy GUI service")
  102. for args, arg_dict in cls.__GUI_ARGS.items():
  103. arg = (args[0], cls.__add_taipy_prefix(args[0]), *args[1:])
  104. gui_parser.add_argument(*arg, **arg_dict)
  105. debug_group = gui_parser.add_mutually_exclusive_group()
  106. for arg, arg_dict in cls.__DEBUG_ARGS.items():
  107. debug_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
  108. reloader_group = gui_parser.add_mutually_exclusive_group()
  109. for arg, arg_dict in cls.__RELOADER_ARGS.items():
  110. reloader_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
  111. browser_group = gui_parser.add_mutually_exclusive_group()
  112. for arg, arg_dict in cls.__BROWSER_ARGS.items():
  113. browser_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
  114. dark_light_mode_group = gui_parser.add_mutually_exclusive_group()
  115. for arg, arg_dict in cls.__DARK_LIGHT_MODE_ARGS.items():
  116. dark_light_mode_group.add_argument(arg, cls.__add_taipy_prefix(arg), **arg_dict)
  117. if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
  118. hook_group = gui_parser.add_mutually_exclusive_group()
  119. for hook_arg, hook_arg_dict in hook_cli_arg.items():
  120. hook_group.add_argument(hook_arg, cls.__add_taipy_prefix(hook_arg), **hook_arg_dict)
  121. @classmethod
  122. def create_run_parser(cls):
  123. run_parser = _TaipyParser._add_subparser("run", help="Run a Taipy application.")
  124. for args, arg_dict in cls.__GUI_ARGS.items():
  125. run_parser.add_argument(*args, **arg_dict)
  126. debug_group = run_parser.add_mutually_exclusive_group()
  127. for arg, arg_dict in cls.__DEBUG_ARGS.items():
  128. debug_group.add_argument(arg, **arg_dict)
  129. reloader_group = run_parser.add_mutually_exclusive_group()
  130. for arg, arg_dict in cls.__RELOADER_ARGS.items():
  131. reloader_group.add_argument(arg, **arg_dict)
  132. browser_group = run_parser.add_mutually_exclusive_group()
  133. for arg, arg_dict in cls.__BROWSER_ARGS.items():
  134. browser_group.add_argument(arg, **arg_dict)
  135. dark_light_mode_group = run_parser.add_mutually_exclusive_group()
  136. for arg, arg_dict in cls.__DARK_LIGHT_MODE_ARGS.items():
  137. dark_light_mode_group.add_argument(arg, **arg_dict)
  138. if (hook_cli_arg := _Hooks()._get_cli_args()) is not None:
  139. hook_group = run_parser.add_mutually_exclusive_group()
  140. for hook_arg, hook_arg_dict in hook_cli_arg.items():
  141. hook_group.add_argument(hook_arg, **hook_arg_dict)
  142. @classmethod
  143. def handle_command(cls):
  144. args, _ = _TaipyParser._parser.parse_known_args()
  145. return args
  146. @classmethod
  147. def __add_taipy_prefix(cls, key: str):
  148. if key.startswith("--no-"):
  149. return key[:5] + "taipy-" + key[5:]
  150. return key[:2] + "taipy-" + key[2:]