Skip to content

Commit

Permalink
[FIX] [ENHANCEMENT] logging records in notebook without ipython (#4988)
Browse files Browse the repository at this point in the history
This PR introduces these results in a notebook environment:


![image](https://github.com/argilla-io/argilla/assets/19620375/9e1a9380-d390-4528-a0b0-154909c83798)

- tqdm shows a status bar
- non info level logging is printed with rich
- `DatasetRecords.log` returns self not `List[Record]` 
- html repr of resources is dealt with as string not ipython `HTML`

# Argilla Community Growers

Thanks for your contribution! As part of our Community Growers
initiative 🌱, we're donating Justdiggit bunds in your name to reforest
sub-Saharan Africa. To claim your Community Growers certificate, please
contact David Berenstein in our Slack community or fill in this form
https://tally.so/r/n9XrxK once your PR has been merged.

# Pull Request Templates

Please go the the `Preview` tab and select the appropriate sub-template:

* [🐞-bug](?expand=1&template=bug.md)
* [📚-documentation](?expand=1&template=docs.md)
* [🆕-features](?expand=1&template=features.md)

# Generic Pull Request Template

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context. List any dependencies that
are required for this change.

Closes #<issue_number>

**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Refactor (change restructuring the codebase without changing
functionality)
- [ ] Improvement (change adding some improvement to an existing
functionality)
- [ ] Documentation update

**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- [ ] Test A
- [ ] Test B

**Checklist**

- [ ] I added relevant documentation
- [ ] follows the style guidelines of this project
- [ ] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [ ] I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
burtenshaw and pre-commit-ci[bot] authored Jun 13, 2024
1 parent 44da3d2 commit 8e9f42b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions argilla-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dynamic = ["version"]
dependencies = [
"httpx>=0.26.0",
"pydantic>=2.6.0, <3.0.0",
"tqdm>=4.60.0",
"rich>=10.0.0",
]

[project.optional-dependencies]
Expand Down
19 changes: 18 additions & 1 deletion argilla-sdk/src/argilla_sdk/_helpers/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from rich import print

import logging

Expand All @@ -36,10 +37,26 @@ def log_message(message: str, level: str = "info") -> None:
logger.log(level=level_int, msg=message)


def log_interactive(message: str) -> None:
"""Log a message to the console in an interactive environment.
Args:
message (str): The message to log.
"""
print(message)


class LoggingMixin:
"""A utility mixin for logging from a `Resource` class."""

def _log_message(self, message: str, level: str = "info") -> None:
class_name = self.__class__.__name__
message = f"{class_name}: {message}"
log_message(level=level, message=message)
if self._is_interactive() and level != "info":
log_interactive(message=message)
else:
log_message(level=level, message=message)

def _is_interactive(self) -> bool:
import __main__ as main

return not hasattr(main, "__file__")
7 changes: 2 additions & 5 deletions argilla-sdk/src/argilla_sdk/_helpers/_resource_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

from typing import Any, Dict

from IPython.display import HTML


RESOURCE_REPR_CONFIG = {
"Dataset": {
"columns": ["name", "id", "workspace_id", "updated_at"],
Expand Down Expand Up @@ -53,7 +50,7 @@ def _resource_to_table_name(self, resource) -> str:
resource_name = resource.__class__.__name__
return RESOURCE_REPR_CONFIG[resource_name]["table_name"]

def _represent_as_html(self, resources) -> HTML:
def _represent_as_html(self, resources) -> str:
table_name = self._resource_to_table_name(resources[0])
table_rows = [self._resource_to_table_row(resource) for resource in resources]

Expand All @@ -69,4 +66,4 @@ def _represent_as_html(self, resources) -> HTML:
html_table += "</tr>"

html_table += "</table>"
return HTML(html_table)._repr_html_()
return html_table
7 changes: 3 additions & 4 deletions argilla-sdk/src/argilla_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from argilla_sdk import Dataset
from argilla_sdk import User

from IPython.display import HTML

__all__ = ["Argilla"]

Expand Down Expand Up @@ -170,7 +169,7 @@ def list(self, workspace: Optional["Workspace"] = None) -> List["User"]:
# Private methods
############################

def _repr_html_(self) -> "HTML":
def _repr_html_(self) -> str:
return self._represent_as_html(resources=self.list())

def _from_model(self, model: UserModel) -> "User":
Expand Down Expand Up @@ -249,7 +248,7 @@ def default(self) -> "Workspace":
# Private methods
############################

def _repr_html_(self) -> "HTML":
def _repr_html_(self) -> str:
return self._represent_as_html(resources=self.list())

def _from_model(self, model: WorkspaceModel) -> "Workspace":
Expand Down Expand Up @@ -324,7 +323,7 @@ def list(self) -> List["Dataset"]:
# Private methods
############################

def _repr_html_(self) -> "HTML":
def _repr_html_(self) -> str:
return self._represent_as_html(resources=self.list())

def _from_model(self, model: DatasetModel) -> "Dataset":
Expand Down
6 changes: 5 additions & 1 deletion argilla-sdk/src/argilla_sdk/records/_dataset_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Sequence, Union
from uuid import UUID

from tqdm import tqdm

from argilla_sdk._api import RecordsAPI
from argilla_sdk._helpers import LoggingMixin
from argilla_sdk._models import RecordModel, MetadataValue
Expand Down Expand Up @@ -224,7 +226,9 @@ def log(

created_or_updated = []
records_updated = 0
for batch in range(0, len(records), batch_size):
for batch in tqdm(
iterable=range(0, len(records), batch_size), desc="Adding and updating records", unit="batch"
):
self._log_message(message=f"Sending records from {batch} to {batch + batch_size}.")
batch_records = record_models[batch : batch + batch_size]
models, updated = self._api.bulk_upsert(dataset_id=self.__dataset.id, records=batch_records)
Expand Down

0 comments on commit 8e9f42b

Please sign in to comment.