_gui_cli.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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._cli._base_cli._abstract_cli import _AbstractCLI
  13. from taipy._cli._base_cli._taipy_parser import _TaipyParser
  14. class _GuiCLI(_AbstractCLI):
  15. """Command-line interface of GUI."""
  16. __GUI_ARGS: Dict[Tuple, Dict] = {
  17. ("--port", "-P"): {
  18. "dest": "taipy_port",
  19. "metavar": "PORT",
  20. "nargs": "?",
  21. "default": "",
  22. "const": "",
  23. "help": "Specify server port",
  24. },
  25. ("--host", "-H"): {
  26. "dest": "taipy_host",
  27. "metavar": "HOST",
  28. "nargs": "?",
  29. "default": "",
  30. "const": "",
  31. "help": "Specify server host",
  32. },
  33. ("--client-url", "-H"): {
  34. "dest": "taipy_client_url",
  35. "metavar": "CLIENT_URL",
  36. "nargs": "?",
  37. "default": "",
  38. "const": "",
  39. "help": "Specify client url",
  40. },
  41. ("--ngrok-token",): {
  42. "dest": "taipy_ngrok_token",
  43. "metavar": "NGROK_TOKEN",
  44. "nargs": "?",
  45. "default": "",
  46. "const": "",
  47. "help": "Specify NGROK Authtoken",
  48. },
  49. ("--webapp-path",): {
  50. "dest": "taipy_webapp_path",
  51. "metavar": "WEBAPP_PATH",
  52. "nargs": "?",
  53. "default": "",
  54. "const": "",
  55. "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
  56. },
  57. ("--upload-folder",): {
  58. "dest": "taipy_upload_folder",
  59. "metavar": "UPLOAD_FOLDER",
  60. "nargs": "?",
  61. "default": "",
  62. "const": "",
  63. "help": "The path to the folder where uploaded files from Taipy GUI will be stored.",
  64. },
  65. }
  66. __DEBUG_ARGS: Dict[str, Dict] = {
  67. "--debug": {"dest": "taipy_debug", "help": "Turn on debug", "action": "store_true"},
  68. "--no-debug": {"dest": "taipy_no_debug", "help": "Turn off debug", "action": "store_true"},
  69. }
  70. __RELOADER_ARGS: Dict[str, Dict] = {
  71. "--use-reloader": {"dest": "taipy_use_reloader", "help": "Auto reload on code changes", "action": "store_true"},
  72. "--no-reloader": {"dest": "taipy_no_reloader", "help": "No reload on code changes", "action": "store_true"},
  73. }
  74. @classmethod
  75. def create_parser(cls):
  76. gui_parser = _TaipyParser._add_groupparser("Taipy GUI", "Optional arguments for Taipy GUI service")
  77. for args, arg_dict in cls.__GUI_ARGS.items():
  78. taipy_arg = (args[0], cls.__add_taipy_prefix(args[0]), *args[1:])
  79. gui_parser.add_argument(*taipy_arg, **arg_dict)
  80. debug_group = gui_parser.add_mutually_exclusive_group()
  81. for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
  82. debug_group.add_argument(debug_arg, cls.__add_taipy_prefix(debug_arg), **debug_arg_dict)
  83. reloader_group = gui_parser.add_mutually_exclusive_group()
  84. for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
  85. reloader_group.add_argument(reloader_arg, cls.__add_taipy_prefix(reloader_arg), **reloader_arg_dict)
  86. @classmethod
  87. def create_run_parser(cls):
  88. run_parser = _TaipyParser._add_subparser("run", help="Run a Taipy application.")
  89. for args, arg_dict in cls.__GUI_ARGS.items():
  90. run_parser.add_argument(*args, **arg_dict)
  91. debug_group = run_parser.add_mutually_exclusive_group()
  92. for debug_arg, debug_arg_dict in cls.__DEBUG_ARGS.items():
  93. debug_group.add_argument(debug_arg, **debug_arg_dict)
  94. reloader_group = run_parser.add_mutually_exclusive_group()
  95. for reloader_arg, reloader_arg_dict in cls.__RELOADER_ARGS.items():
  96. reloader_group.add_argument(reloader_arg, **reloader_arg_dict)
  97. @classmethod
  98. def handle_command(cls):
  99. args, _ = _TaipyParser._parser.parse_known_args()
  100. return args
  101. @classmethod
  102. def __add_taipy_prefix(cls, key: str):
  103. if key.startswith("--no-"):
  104. return key[:5] + "taipy-" + key[5:]
  105. return key[:2] + "taipy-" + key[2:]