Skip to content

Commit f2b2b13

Browse files
Merge pull request #2008 from kili-technology/feature/lab-4112-aau-i-deactivate-the-progress-bar-when-cancelling-issues
feat(LAB-4112): add tqdm management for cancelling issues
2 parents fed3b7b + 12262b7 commit f2b2b13

File tree

2 files changed

+78
-32
lines changed

2 files changed

+78
-32
lines changed

src/kili/client_domain.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
api_endpoint: Optional[str] = None,
4949
verify: Optional[Union[bool, str]] = None,
5050
graphql_client_params: Optional[GraphQLClientParams] = None,
51+
disable_tqdm: bool | None = None,
5152
) -> None:
5253
"""Initialize Kili client (domain mode).
5354
@@ -73,6 +74,10 @@ def __init__(
7374
man-in-the-middle (MitM) attacks. Setting verify to ``False``
7475
may be useful during local development or testing.
7576
graphql_client_params: Parameters to pass to the graphQL client.
77+
disable_tqdm: Global setting to disable progress bars (tqdm) for all operations.
78+
Can be overridden by individual function calls.
79+
Default to `KILI_DISABLE_TQDM` environment variable.
80+
If not passed, default to `disable_tqdm` in config file or False.
7681
7782
Returns:
7883
Instance of the Kili client.
@@ -86,6 +91,11 @@ def __init__(
8691
kili.assets # domain namespace (clean name)
8792
kili.projects.list() # domain methods
8893
```
94+
95+
Disable progress bars globally:
96+
```python
97+
kili = Kili(disable_tqdm=True)
98+
```
8999
"""
90100
warnings.warn(
91101
"Client domain api is still a work in progress. Method names and return type will evolve.",
@@ -97,6 +107,7 @@ def __init__(
97107
verify,
98108
GraphQLClientName.SDK_DOMAIN,
99109
graphql_client_params,
110+
disable_tqdm,
100111
)
101112

102113
# Domain API Namespaces - Lazy loaded properties

src/kili/domain_api/issues.py

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
from kili.domain_api.namespace_utils import get_available_methods
2121
from kili.presentation.client.helpers.common_validators import (
2222
assert_all_arrays_have_same_size,
23+
resolve_disable_tqdm,
2324
)
2425
from kili.use_cases.issue import IssueUseCases
2526
from kili.use_cases.issue.types import IssueToCreateUseCaseInput
27+
from kili.utils import tqdm
2628

2729

2830
class IssueFilter(TypedDict, total=False):
@@ -367,6 +369,7 @@ def cancel(
367369
*,
368370
issue_id: Optional[str] = None,
369371
issue_ids: Optional[List[str]] = None,
372+
disable_tqdm: Optional[bool] = None,
370373
) -> List[dict[str, Any]]:
371374
"""Cancel issues by setting their status to CANCELLED.
372375
@@ -377,6 +380,7 @@ def cancel(
377380
Args:
378381
issue_id: Issue ID to cancel.
379382
issue_ids: List of issue IDs to cancel.
383+
disable_tqdm: If `True`, the progress bar will be disabled.
380384
381385
Returns:
382386
List of dictionaries with the results of the status updates.
@@ -399,21 +403,32 @@ def cancel(
399403

400404
assert issue_ids is not None, "issue_ids must be provided"
401405

406+
resolved_disable_tqdm = resolve_disable_tqdm(disable_tqdm, self._client.disable_tqdm)
407+
402408
issue_use_cases = IssueUseCases(self._gateway)
403409
results = []
404410

405-
for issue_id_item in issue_ids:
406-
try:
407-
result = issue_use_cases.update_issue_status(
408-
issue_id=IssueId(issue_id_item), status="CANCELLED"
409-
)
410-
results.append(
411-
{"id": issue_id_item, "status": "CANCELLED", "success": True, **result}
412-
)
413-
except (ValueError, TypeError, RuntimeError) as e:
414-
results.append(
415-
{"id": issue_id_item, "status": "CANCELLED", "success": False, "error": str(e)}
416-
)
411+
with tqdm.tqdm(
412+
total=len(issue_ids), disable=resolved_disable_tqdm, desc="Cancelling issues"
413+
) as pbar:
414+
for issue_id_item in issue_ids:
415+
try:
416+
result = issue_use_cases.update_issue_status(
417+
issue_id=IssueId(issue_id_item), status="CANCELLED"
418+
)
419+
results.append(
420+
{"id": issue_id_item, "status": "CANCELLED", "success": True, **result}
421+
)
422+
except (ValueError, TypeError, RuntimeError) as e:
423+
results.append(
424+
{
425+
"id": issue_id_item,
426+
"status": "CANCELLED",
427+
"success": False,
428+
"error": str(e),
429+
}
430+
)
431+
pbar.update(1)
417432

418433
return results
419434

@@ -431,6 +446,7 @@ def open(
431446
*,
432447
issue_id: Optional[str] = None,
433448
issue_ids: Optional[List[str]] = None,
449+
disable_tqdm: Optional[bool] = None,
434450
) -> List[dict[str, Any]]:
435451
"""Open issues by setting their status to OPEN.
436452
@@ -441,6 +457,7 @@ def open(
441457
Args:
442458
issue_id: Issue ID to open.
443459
issue_ids: List of issue IDs to open.
460+
disable_tqdm: If `True`, the progress bar will be disabled.
444461
445462
Returns:
446463
List of dictionaries with the results of the status updates.
@@ -463,19 +480,27 @@ def open(
463480

464481
assert issue_ids is not None, "issue_ids must be provided"
465482

483+
resolved_disable_tqdm = resolve_disable_tqdm(disable_tqdm, self._client.disable_tqdm)
484+
466485
issue_use_cases = IssueUseCases(self._gateway)
467486
results = []
468487

469-
for issue_id_item in issue_ids:
470-
try:
471-
result = issue_use_cases.update_issue_status(
472-
issue_id=IssueId(issue_id_item), status="OPEN"
473-
)
474-
results.append({"id": issue_id_item, "status": "OPEN", "success": True, **result})
475-
except (ValueError, TypeError, RuntimeError) as e:
476-
results.append(
477-
{"id": issue_id_item, "status": "OPEN", "success": False, "error": str(e)}
478-
)
488+
with tqdm.tqdm(
489+
total=len(issue_ids), disable=resolved_disable_tqdm, desc="Opening issues"
490+
) as pbar:
491+
for issue_id_item in issue_ids:
492+
try:
493+
result = issue_use_cases.update_issue_status(
494+
issue_id=IssueId(issue_id_item), status="OPEN"
495+
)
496+
results.append(
497+
{"id": issue_id_item, "status": "OPEN", "success": True, **result}
498+
)
499+
except (ValueError, TypeError, RuntimeError) as e:
500+
results.append(
501+
{"id": issue_id_item, "status": "OPEN", "success": False, "error": str(e)}
502+
)
503+
pbar.update(1)
479504

480505
return results
481506

@@ -493,6 +518,7 @@ def solve(
493518
*,
494519
issue_id: Optional[str] = None,
495520
issue_ids: Optional[List[str]] = None,
521+
disable_tqdm: Optional[bool] = None,
496522
) -> List[dict[str, Any]]:
497523
"""Solve issues by setting their status to SOLVED.
498524
@@ -503,6 +529,7 @@ def solve(
503529
Args:
504530
issue_id: Issue ID to solve.
505531
issue_ids: List of issue IDs to solve.
532+
disable_tqdm: If `True`, the progress bar will be disabled.
506533
507534
Returns:
508535
List of dictionaries with the results of the status updates.
@@ -525,19 +552,27 @@ def solve(
525552

526553
assert issue_ids is not None, "issue_ids must be provided"
527554

555+
resolved_disable_tqdm = resolve_disable_tqdm(disable_tqdm, self._client.disable_tqdm)
556+
528557
issue_use_cases = IssueUseCases(self._gateway)
529558
results = []
530559

531-
for issue_id_item in issue_ids:
532-
try:
533-
result = issue_use_cases.update_issue_status(
534-
issue_id=IssueId(issue_id_item), status="SOLVED"
535-
)
536-
results.append({"id": issue_id_item, "status": "SOLVED", "success": True, **result})
537-
except (ValueError, TypeError, RuntimeError) as e:
538-
results.append(
539-
{"id": issue_id_item, "status": "SOLVED", "success": False, "error": str(e)}
540-
)
560+
with tqdm.tqdm(
561+
total=len(issue_ids), disable=resolved_disable_tqdm, desc="Solving issues"
562+
) as pbar:
563+
for issue_id_item in issue_ids:
564+
try:
565+
result = issue_use_cases.update_issue_status(
566+
issue_id=IssueId(issue_id_item), status="SOLVED"
567+
)
568+
results.append(
569+
{"id": issue_id_item, "status": "SOLVED", "success": True, **result}
570+
)
571+
except (ValueError, TypeError, RuntimeError) as e:
572+
results.append(
573+
{"id": issue_id_item, "status": "SOLVED", "success": False, "error": str(e)}
574+
)
575+
pbar.update(1)
541576

542577
return results
543578

0 commit comments

Comments
 (0)