28 lines
847 B
Python
28 lines
847 B
Python
def get_tqdm(enable: bool = True):
|
|
"""
|
|
返回适用于当前环境的 tqdm 对象。
|
|
|
|
Args:
|
|
enable (bool): 是否启用进度条。默认为 True。
|
|
|
|
Returns:
|
|
tqdm 对象。
|
|
"""
|
|
if not enable:
|
|
# 如果进度条被禁用,返回一个不显示进度条的 tqdm 对象
|
|
return lambda iterable, *args, **kwargs: iterable
|
|
|
|
try:
|
|
# 尝试检查是否在 jupyter notebook 环境中,有利于退出进度条
|
|
# noinspection PyUnresolvedReferences
|
|
shell = get_ipython().__class__.__name__
|
|
if shell == "ZMQInteractiveShell":
|
|
from tqdm.notebook import tqdm
|
|
else:
|
|
from tqdm import tqdm
|
|
except (NameError, ImportError):
|
|
# 如果不在 Jupyter 环境中,就使用标准 tqdm
|
|
from tqdm import tqdm
|
|
|
|
return tqdm
|