console.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """Functions to communicate to the user via console."""
  2. from __future__ import annotations
  3. from rich.console import Console
  4. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  5. from rich.prompt import Prompt
  6. from reflex.constants import LogLevel
  7. # Console for pretty printing.
  8. _console = Console()
  9. # The current log level.
  10. _LOG_LEVEL = LogLevel.INFO
  11. # Deprecated features who's warning has been printed.
  12. _EMITTED_DEPRECATION_WARNINGS = set()
  13. def set_log_level(log_level: LogLevel):
  14. """Set the log level.
  15. Args:
  16. log_level: The log level to set.
  17. """
  18. global _LOG_LEVEL
  19. _LOG_LEVEL = log_level
  20. def is_debug() -> bool:
  21. """Check if the log level is debug.
  22. Returns:
  23. True if the log level is debug.
  24. """
  25. return _LOG_LEVEL <= LogLevel.DEBUG
  26. def print(msg: str, **kwargs):
  27. """Print a message.
  28. Args:
  29. msg: The message to print.
  30. kwargs: Keyword arguments to pass to the print function.
  31. """
  32. _console.print(msg, **kwargs)
  33. def debug(msg: str, **kwargs):
  34. """Print a debug message.
  35. Args:
  36. msg: The debug message.
  37. kwargs: Keyword arguments to pass to the print function.
  38. """
  39. if is_debug():
  40. msg_ = f"[blue]Debug: {msg}[/blue]"
  41. if progress := kwargs.pop("progress", None):
  42. progress.console.print(msg_, **kwargs)
  43. else:
  44. print(msg_, **kwargs)
  45. def info(msg: str, **kwargs):
  46. """Print an info message.
  47. Args:
  48. msg: The info message.
  49. kwargs: Keyword arguments to pass to the print function.
  50. """
  51. if _LOG_LEVEL <= LogLevel.INFO:
  52. print(f"[cyan]Info: {msg}[/cyan]", **kwargs)
  53. def success(msg: str, **kwargs):
  54. """Print a success message.
  55. Args:
  56. msg: The success message.
  57. kwargs: Keyword arguments to pass to the print function.
  58. """
  59. if _LOG_LEVEL <= LogLevel.INFO:
  60. print(f"[green]Success: {msg}[/green]", **kwargs)
  61. def log(msg: str, **kwargs):
  62. """Takes a string and logs it to the console.
  63. Args:
  64. msg: The message to log.
  65. kwargs: Keyword arguments to pass to the print function.
  66. """
  67. if _LOG_LEVEL <= LogLevel.INFO:
  68. _console.log(msg, **kwargs)
  69. def rule(title: str, **kwargs):
  70. """Prints a horizontal rule with a title.
  71. Args:
  72. title: The title of the rule.
  73. kwargs: Keyword arguments to pass to the print function.
  74. """
  75. _console.rule(title, **kwargs)
  76. def warn(msg: str, **kwargs):
  77. """Print a warning message.
  78. Args:
  79. msg: The warning message.
  80. kwargs: Keyword arguments to pass to the print function.
  81. """
  82. if _LOG_LEVEL <= LogLevel.WARNING:
  83. print(f"[orange1]Warning: {msg}[/orange1]", **kwargs)
  84. def deprecate(
  85. feature_name: str,
  86. reason: str,
  87. deprecation_version: str,
  88. removal_version: str,
  89. dedupe: bool = True,
  90. **kwargs,
  91. ):
  92. """Print a deprecation warning.
  93. Args:
  94. feature_name: The feature to deprecate.
  95. reason: The reason for deprecation.
  96. deprecation_version: The version the feature was deprecated
  97. removal_version: The version the deprecated feature will be removed
  98. dedupe: If True, suppress multiple console logs of deprecation message.
  99. kwargs: Keyword arguments to pass to the print function.
  100. """
  101. if feature_name not in _EMITTED_DEPRECATION_WARNINGS:
  102. msg = (
  103. f"{feature_name} has been deprecated in version {deprecation_version} {reason.rstrip('.')}. It will be completely "
  104. f"removed in {removal_version}"
  105. )
  106. if _LOG_LEVEL <= LogLevel.WARNING:
  107. print(f"[yellow]DeprecationWarning: {msg}[/yellow]", **kwargs)
  108. if dedupe:
  109. _EMITTED_DEPRECATION_WARNINGS.add(feature_name)
  110. def error(msg: str, **kwargs):
  111. """Print an error message.
  112. Args:
  113. msg: The error message.
  114. kwargs: Keyword arguments to pass to the print function.
  115. """
  116. if _LOG_LEVEL <= LogLevel.ERROR:
  117. print(f"[red]{msg}[/red]", **kwargs)
  118. def ask(
  119. question: str,
  120. choices: list[str] | None = None,
  121. default: str | None = None,
  122. show_choices: bool = True,
  123. ) -> str:
  124. """Takes a prompt question and optionally a list of choices
  125. and returns the user input.
  126. Args:
  127. question: The question to ask the user.
  128. choices: A list of choices to select from.
  129. default: The default option selected.
  130. show_choices: Whether to show the choices.
  131. Returns:
  132. A string with the user input.
  133. """
  134. return Prompt.ask(
  135. question, choices=choices, default=default, show_choices=show_choices
  136. ) # type: ignore
  137. def progress():
  138. """Create a new progress bar.
  139. Returns:
  140. A new progress bar.
  141. """
  142. return Progress(
  143. *Progress.get_default_columns()[:-1],
  144. MofNCompleteColumn(),
  145. TimeElapsedColumn(),
  146. )
  147. def status(*args, **kwargs):
  148. """Create a status with a spinner.
  149. Args:
  150. *args: Args to pass to the status.
  151. **kwargs: Kwargs to pass to the status.
  152. Returns:
  153. A new status.
  154. """
  155. return _console.status(*args, **kwargs)