console.py 4.5 KB

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