Skip to content

Commit

Permalink
adding a ProgressHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed Jan 3, 2024
1 parent 115198e commit e53abdc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/source/plugins/developing_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,26 @@ using the pattern show below:
ctx.dataset.add_images_dir(images_dir, progress=progress)
You can also use the builtin
:class:`ProgressHandler <fiftyone.operators.ProgressHandler>` class to
automatically forward logging messages to
:meth:`set_progress() <fiftyone.operators.executor.ExecutionContext.set_progress>`
as `label` values using the pattern shown below:

.. code-block:: python
:linenos:
import logging
import fiftyone.operators as foo
import fiftyone.zoo as foz
def execute(self, ctx):
name = ctx.params["name"]
# Automatically report all `fiftyone` logging messages
with foo.ProgressHandler(ctx, logger=logging.getLogger("fiftyone")):
foz.load_zoo_dataset(name, persistent=True)
.. _operator-execution:

Operator execution
Expand Down
1 change: 1 addition & 0 deletions fiftyone/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
execute_operator,
ExecutionOptions,
)
from .utils import ProgressHandler

# This enables Sphinx refs to directly use paths imported here
__all__ = [k for k, v in globals().items() if not k.startswith("_")]
50 changes: 50 additions & 0 deletions fiftyone/operators/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
FiftyOne operator utilities.
| Copyright 2017-2023, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""
import logging


class ProgressHandler(logging.Handler):
"""A logging handler that reports all logging messages issued while the
handler's context manager is active to the provided execution context's
:meth:`set_progress() <fiftyone.operators.executor.ExecutionContext.set_progress>`
method.
Args:
ctx: an :class:`fiftyone.operators.executor.ExecutionContext`
logger (None): a specific ``logging.Logger`` for which to report
records. By default, the root logger is used
level (None): an optional logging level above which to report records.
By default, the logger's effective level is used
"""

def __init__(self, ctx, logger=None, level=None):
super().__init__()
self.ctx = ctx
self.logger = logger
self.level = level

def __enter__(self):
if self.logger is None:
self.logger = logging.getLogger()

if self.level is None:
self.level = self.logger.getEffectiveLevel()

self.setLevel(self.level)
self.logger.addHandler(self)

def __exit__(self, *args):
try:
self.logger.removeHandler(self)
except:
pass

def emit(self, record):
msg = self.format(record)
print(f"****** {msg} ******")
self.ctx.set_progress(label=msg)

0 comments on commit e53abdc

Please sign in to comment.