From bc3094cab0c1fa405b189ad43adc31d61c1632a1 Mon Sep 17 00:00:00 2001 From: Jason Munro Date: Fri, 4 Mar 2022 12:29:07 -0800 Subject: [PATCH] Mute progress bars with `MUTE_PROGRESS_BARS` env variable (#545) * Enable progress bar mute with env variable * Temp remove windows test * Temp remove macos testing --- .github/workflows/testing.yml | 2 +- src/mp_api/core/client.py | 23 ++++++++++++++++------- src/mp_api/core/settings.py | 4 ++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 36624e25..46ff38d5 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -48,7 +48,7 @@ jobs: strategy: max-parallel: 2 matrix: - os: [ubuntu-latest, windows-latest, macos-latest] + os: [ubuntu-latest] python-version: [3.8, 3.9] runs-on: ${{ matrix.os }} diff --git a/src/mp_api/core/client.py b/src/mp_api/core/client.py index 7dbd3c4e..25102b61 100644 --- a/src/mp_api/core/client.py +++ b/src/mp_api/core/client.py @@ -476,9 +476,13 @@ def _submit_requests( num_docs_needed = min((max_pages * chunk_size), total_num_docs) # Setup progress bar - pbar = tqdm( - desc=f"Retrieving {self.document_model.__name__} documents", # type: ignore - total=num_docs_needed, + pbar = ( + tqdm( + desc=f"Retrieving {self.document_model.__name__} documents", # type: ignore + total=num_docs_needed, + ) + if not MAPIClientSettings().MUTE_PROGRESS_BARS + else None ) initial_data_length = len(total_data["data"]) @@ -487,15 +491,18 @@ def _submit_requests( if initial_data_length >= num_docs_needed or num_chunks == 1: new_total_data = copy(total_data) new_total_data["data"] = total_data["data"][:num_docs_needed] - pbar.update(num_docs_needed) - pbar.close() + + if pbar is not None: + pbar.update(num_docs_needed) + pbar.close() return new_total_data # otherwise, prepare to paginate in parallel if chunk_size is None: raise ValueError("A chunk size must be provided to enable pagination") - pbar.update(initial_data_length) + if pbar is not None: + pbar.update(initial_data_length) # Warning to select specific fields only for many results if criteria.get("all_fields", False) and (total_num_docs / chunk_size > 10): @@ -546,7 +553,8 @@ def _submit_requests( if "meta" in data: total_data["meta"]["time_stamp"] = data["meta"]["time_stamp"] - pbar.close() + if pbar is not None: + pbar.close() return total_data @@ -780,6 +788,7 @@ def search( A generic search method to retrieve documents matching specific parameters. Arguments: + mute (bool): Whether to mute progress bars. num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible. chunk_size (int): Number of data entries per chunk. all_fields (bool): Set to False to only return specific fields of interest. This will diff --git a/src/mp_api/core/settings.py b/src/mp_api/core/settings.py index a34e3ba0..0e0a69a5 100644 --- a/src/mp_api/core/settings.py +++ b/src/mp_api/core/settings.py @@ -44,3 +44,7 @@ class MAPIClientSettings(BaseSettings): ) MAX_RETRIES: int = Field(3, description="Maximum number of retries for requests.") + + MUTE_PROGRESS_BARS: bool = Field( + False, description="Whether to mute progress bars when data is retrieved.", + )