-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from christoph-blessing/progress_bar
Add progress bar
- Loading branch information
Showing
18 changed files
with
357 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Contains DataJoint-specific code for relaying progress information to the user.""" | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from collections.abc import Iterable | ||
|
||
from link.domain.custom_types import Identifier | ||
from link.domain.state import Processes | ||
from link.service.progress import ProgessDisplay | ||
|
||
from .identification import IdentificationTranslator | ||
|
||
|
||
class ProgressView(ABC): | ||
"""Progress display.""" | ||
|
||
@abstractmethod | ||
def open(self, description: str, total: int, unit: str) -> None: | ||
"""Open the progress display showing information to the user.""" | ||
|
||
@abstractmethod | ||
def update_current(self, new: str) -> None: | ||
"""Update the display with new information regarding the current iteration.""" | ||
|
||
@abstractmethod | ||
def update_iteration(self) -> None: | ||
"""Update the display to reflect that the current iteration finished.""" | ||
|
||
@abstractmethod | ||
def close(self) -> None: | ||
"""Close the progress display.""" | ||
|
||
@abstractmethod | ||
def enable(self) -> None: | ||
"""Enable the view.""" | ||
|
||
@abstractmethod | ||
def disable(self) -> None: | ||
"""Disable the view.""" | ||
|
||
|
||
class DJProgressDisplayAdapter(ProgessDisplay): | ||
"""DataJoint-specific adapter for the progress display.""" | ||
|
||
def __init__(self, translator: IdentificationTranslator, display: ProgressView) -> None: | ||
"""Initialize the display.""" | ||
self._translator = translator | ||
self._display = display | ||
|
||
def start(self, process: Processes, to_be_processed: Iterable[Identifier]) -> None: | ||
"""Start showing progress information to the user.""" | ||
self._display.open(process.name, len(list(to_be_processed)), "row") | ||
|
||
def update_current(self, new: Identifier) -> None: | ||
"""Update the display to reflect a new entity being currently processed.""" | ||
self._display.update_current(repr(self._translator.to_primary_key(new))) | ||
|
||
def finish_current(self) -> None: | ||
"""Update the display to reflect that the current entity finished processing.""" | ||
self._display.update_iteration() | ||
|
||
def stop(self) -> None: | ||
"""Stop showing progress information to the user.""" | ||
self._display.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Contains views for showing progress information to the user.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import NoReturn | ||
|
||
from tqdm.auto import tqdm | ||
|
||
from link.adapters.progress import ProgressView | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TQDMProgressView(ProgressView): | ||
"""A view that uses tqdm to show a progress bar.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the view.""" | ||
self.__progress_bar: tqdm[NoReturn] | None = None | ||
self._is_disabled: bool = False | ||
|
||
@property | ||
def _progress_bar(self) -> tqdm[NoReturn]: | ||
assert self.__progress_bar | ||
return self.__progress_bar | ||
|
||
def open(self, description: str, total: int, unit: str) -> None: | ||
"""Start showing the progress bar.""" | ||
self.__progress_bar = tqdm(total=total, desc=description, unit=unit, disable=self._is_disabled) | ||
|
||
def update_current(self, new: str) -> None: | ||
"""Update information about the current iteration shown at the end of the bar.""" | ||
self._progress_bar.set_postfix(current=new) | ||
|
||
def update_iteration(self) -> None: | ||
"""Update the bar to show an iteration finished.""" | ||
self._progress_bar.update() | ||
|
||
def close(self) -> None: | ||
"""Stop showing the progress bar.""" | ||
self._progress_bar.close() | ||
self.__progress_bar = None | ||
|
||
def enable(self) -> None: | ||
"""Enable the progress bar.""" | ||
self._is_disabled = False | ||
|
||
def disable(self) -> None: | ||
"""Disable the progress bar.""" | ||
self._is_disabled = True |
Oops, something went wrong.