我正在使用tqdm
包提供python中的进度条。tqdm在不同python环境下的变化
tqdm也有一个Jupyter笔记本电脑的小部件(tqdm_notebook()
),允许一个漂亮的“web-ish”进度条。
我的问题,我有一个code.py
文件内的tqdm进度条,我导入到jupyter笔记本。
当运行code.py
从常规的Python eviroment(即Ipython
,IDLE
,shell
)我想tqdm在正常的形式运行:
from tqdm import tqdm
a = 0
for i in tqdm(range(2000)):
a+=i
,但是当我输入code.py
到Jupyter,我希望它用tqdm_notebook()
:
from tqdm import tqdm_notebook as tqdm
a = 0
for i in tqdm(range(2000)):
a+=i
如何让python区分环境?
我发现this post暗示检查get_ipython().__class__.__name__
或'ipykernel' in sys.modules
但它不会对笔记本电脑和其他IPython的外壳程序(如在Spyder的或IDLE)区分。