Skip to content

Commit

Permalink
conditional import of tqdm_notebook (#13)
Browse files Browse the repository at this point in the history
* conditional import of tqdm_notebook

* added missing widget for notebooks

* fix max filename length in downloads
  • Loading branch information
nocollier authored Jan 22, 2024
1 parent 7edc514 commit cdb8af6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions ci/environment-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- distributed
- netCDF4
- matplotlib
- ipywidgets
- pip
- pip:
- myst_nb
Expand Down
22 changes: 19 additions & 3 deletions intake_esgf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@

import xarray as xr

from intake_esgf.catalog import ESGFCatalog

warnings.simplefilter("ignore", category=xr.SerializationWarning)


__all__ = ["ESGFCatalog"]
def in_notebook() -> bool:
"""Check if the code is running in a jupyter notebook"""
try:
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell": # Jupyter notebook, Spyder or qtconsole
return True
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter


IN_NOTEBOOK = in_notebook()

from intake_esgf.catalog import ESGFCatalog # noqa

__all__ = ["ESGFCatalog", "IN_NOTEBOOK"]
15 changes: 13 additions & 2 deletions intake_esgf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import pandas as pd
import requests
import xarray as xr
from tqdm import tqdm

from intake_esgf import IN_NOTEBOOK
from intake_esgf.database import (
get_download_rate_dataframe,
log_download_information,
Expand All @@ -20,6 +20,11 @@
from intake_esgf.exceptions import NoSearchResults
from intake_esgf.logging import setup_logging

if IN_NOTEBOOK:
from tqdm import tqdm_notebook as tqdm
else:
from tqdm import tqdm

bar_format = "{desc:>20}: {percentage:3.0f}%|{bar}|{n_fmt}/{total_fmt} [{rate_fmt:>15s}{postfix}]"


Expand Down Expand Up @@ -93,6 +98,12 @@ def download_and_verify(
"""Download the url to a local file and check for validity, removing if not."""
if not isinstance(local_file, Path):
local_file = Path(local_file)
max_file_length = 40
desc = (
local_file.name
if len(local_file.name) < max_file_length
else f"{local_file.name[:(max_file_length-3)]}..."
)
local_file.parent.mkdir(parents=True, exist_ok=True)
resp = requests.get(url, stream=True, timeout=10)
resp.raise_for_status()
Expand All @@ -104,7 +115,7 @@ def download_and_verify(
total=content_length,
unit="B",
unit_scale=True,
desc=local_file.name,
desc=desc,
ascii=False,
) as pbar:
for chunk in resp.iter_content(chunk_size=1024):
Expand Down
7 changes: 6 additions & 1 deletion intake_esgf/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import requests
import xarray as xr
from datatree import DataTree
from tqdm import tqdm

from intake_esgf import IN_NOTEBOOK
from intake_esgf.base import (
add_cell_measures,
bar_format,
Expand All @@ -24,6 +24,11 @@
from intake_esgf.database import create_download_database, get_download_rate_dataframe
from intake_esgf.logging import setup_logging

if IN_NOTEBOOK:
from tqdm import tqdm_notebook as tqdm
else:
from tqdm import tqdm


class ESGFCatalog:
"""A data catalog for searching ESGF nodes and downloading data.
Expand Down
7 changes: 6 additions & 1 deletion intake_esgf/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

import pandas as pd
import xarray as xr
from tqdm import tqdm

from intake_esgf import IN_NOTEBOOK
from intake_esgf.base import bar_format, get_cell_measure, get_search_criteria

if IN_NOTEBOOK:
from tqdm import tqdm_notebook as tqdm
else:
from tqdm import tqdm


def global_sum(
dsd: Union[dict[str, xr.Dataset], xr.Dataset], quiet: bool = False
Expand Down

0 comments on commit cdb8af6

Please sign in to comment.