From 1f7d352f10a147b1d079e96a8053f38fea0d6ee7 Mon Sep 17 00:00:00 2001 From: manascb1344 Date: Sun, 7 Jan 2024 15:14:37 +0530 Subject: [PATCH 1/4] Fixed Visibility of progress bar in dark and light theme --- .../callbacks/progress/rich_progress.py | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/lightning/pytorch/callbacks/progress/rich_progress.py b/src/lightning/pytorch/callbacks/progress/rich_progress.py index 4fcca7d0b0e65..12186756ed618 100644 --- a/src/lightning/pytorch/callbacks/progress/rich_progress.py +++ b/src/lightning/pytorch/callbacks/progress/rich_progress.py @@ -23,6 +23,9 @@ from lightning.pytorch.callbacks.progress.progress_bar import ProgressBar from lightning.pytorch.utilities.types import STEP_OUTPUT +import colored +from colored import stylize + _RICH_AVAILABLE = RequirementCache("rich>=10.2.2") if _RICH_AVAILABLE: @@ -220,6 +223,15 @@ class RichProgressBarTheme: metrics_text_delimiter: str = " " metrics_format: str = ".3f" +def detect_color_theme(): + """Detect the color theme of the terminal.""" + if colored.supports_color(): + if colored.detect_color() == 'truecolor': + return 'dark' + else: + return 'light' + else: + return 'unknown' class RichProgressBar(ProgressBar): """Create a progress bar with `rich text formatting `_. @@ -284,6 +296,8 @@ def __init__( self._progress_stopped: bool = False self.theme = theme self._update_for_light_colab_theme() + self._color_theme = detect_color_theme() + self._update_for_light_colab_theme() @property def refresh_rate(self) -> float: @@ -641,16 +655,30 @@ def on_exception( self._stop_progress() def configure_columns(self, trainer: "pl.Trainer") -> list: + # Modify the color of progress bar based on the detected color theme + if self._color_theme == 'dark': + theme = RichProgressBarTheme( + progress_bar="green", + progress_bar_finished="green", + progress_bar_pulse="green", + ) + else: + theme = RichProgressBarTheme( + progress_bar="blue", + progress_bar_finished="blue", + progress_bar_pulse="blue", + ) + return [ TextColumn("[progress.description]{task.description}"), CustomBarColumn( - complete_style=self.theme.progress_bar, - finished_style=self.theme.progress_bar_finished, - pulse_style=self.theme.progress_bar_pulse, + complete_style=theme.progress_bar, + finished_style=theme.progress_bar_finished, + pulse_style=theme.progress_bar_pulse, ), - BatchesProcessedColumn(style=self.theme.batch_progress), - CustomTimeColumn(style=self.theme.time), - ProcessingSpeedColumn(style=self.theme.processing_speed), + BatchesProcessedColumn(style=theme.batch_progress), + CustomTimeColumn(style=theme.time), + ProcessingSpeedColumn(style=theme.processing_speed), ] def __getstate__(self) -> Dict: From 92aeff97181e08a23ae84a15681dbf3e1cccd19f Mon Sep 17 00:00:00 2001 From: manascb1344 Date: Sun, 7 Jan 2024 15:42:45 +0530 Subject: [PATCH 2/4] Added the library used in previous commit --- requirements/typing.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/typing.txt b/requirements/typing.txt index 33fdbc2bd8ed1..862465f163e26 100644 --- a/requirements/typing.txt +++ b/requirements/typing.txt @@ -1,5 +1,6 @@ mypy==1.5.1 torch==2.1.0 +colored types-Markdown types-PyYAML From 3acea1185eb7a02c82720c1d1430db4d4f463c2a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 7 Jan 2024 10:16:17 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../pytorch/callbacks/progress/rich_progress.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lightning/pytorch/callbacks/progress/rich_progress.py b/src/lightning/pytorch/callbacks/progress/rich_progress.py index 12186756ed618..62829726ecaf0 100644 --- a/src/lightning/pytorch/callbacks/progress/rich_progress.py +++ b/src/lightning/pytorch/callbacks/progress/rich_progress.py @@ -16,6 +16,7 @@ from datetime import timedelta from typing import Any, Dict, Generator, Optional, Union, cast +import colored from lightning_utilities.core.imports import RequirementCache from typing_extensions import override @@ -23,9 +24,6 @@ from lightning.pytorch.callbacks.progress.progress_bar import ProgressBar from lightning.pytorch.utilities.types import STEP_OUTPUT -import colored -from colored import stylize - _RICH_AVAILABLE = RequirementCache("rich>=10.2.2") if _RICH_AVAILABLE: @@ -223,15 +221,17 @@ class RichProgressBarTheme: metrics_text_delimiter: str = " " metrics_format: str = ".3f" + def detect_color_theme(): """Detect the color theme of the terminal.""" if colored.supports_color(): - if colored.detect_color() == 'truecolor': - return 'dark' + if colored.detect_color() == "truecolor": + return "dark" else: - return 'light' + return "light" else: - return 'unknown' + return "unknown" + class RichProgressBar(ProgressBar): """Create a progress bar with `rich text formatting `_. @@ -656,7 +656,7 @@ def on_exception( def configure_columns(self, trainer: "pl.Trainer") -> list: # Modify the color of progress bar based on the detected color theme - if self._color_theme == 'dark': + if self._color_theme == "dark": theme = RichProgressBarTheme( progress_bar="green", progress_bar_finished="green", From 6d33cb5a51ba900f7f4d3a8fcd51f6c9094651d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Feb 2024 17:22:50 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/lightning/pytorch/callbacks/progress/rich_progress.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lightning/pytorch/callbacks/progress/rich_progress.py b/src/lightning/pytorch/callbacks/progress/rich_progress.py index 62829726ecaf0..fddb3469c3c8f 100644 --- a/src/lightning/pytorch/callbacks/progress/rich_progress.py +++ b/src/lightning/pytorch/callbacks/progress/rich_progress.py @@ -227,10 +227,8 @@ def detect_color_theme(): if colored.supports_color(): if colored.detect_color() == "truecolor": return "dark" - else: - return "light" - else: - return "unknown" + return "light" + return "unknown" class RichProgressBar(ProgressBar):