Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tqdm import check if jupyter notebook or lab is running

I have a module (tqdm) that I need to import differently depending on whether I'm running my .ipynb in a jupyter notebook or jupyter lab environment. Is there way that I can determine this in python? For example:

if <jupyter notebook>:
    from tqdm import tqdm_notebook as tqdm 
elif <jupyter lab>:
    from tqdm import tqdm
else:
    print("Not in jupyter environment.")
like image 771
Austin Avatar asked Jun 18 '18 19:06

Austin


People also ask

How do I know if a Jupyter notebook is running?

Note: You can also tell whether a cell is currently executing in a Jupyter notebook by inspecting the small circle in the top-right of the window. The circle will turn grey (“Kernel busy”) when the cell is running, and return to empty (“Kernel idle”) when the process is complete.

How do I know if I have JupyterLab installed?

If you wish to know where Jupyter isinstalled on your computer, you may run where jupyter in the Command prompt. If you wish to know which Python version is installed, run python or python -V or python --version .

Does tqdm work in Jupyter notebook?

TQDM is a progress bar library with good support for nested loops and Jupyter/IPython notebooks.


1 Answers

# either:
from tqdm.autonotebook import tqdm
# or to suppress the warning:
from tqdm.auto import tqdm

For other modules/checks, see How can I check if code is executed in the IPython notebook? (But note that the accepted albeit unpopular answer there is "this is intentionally not meant to be possible by design.")

like image 166
casper.dcl Avatar answered Oct 22 '22 12:10

casper.dcl