0

With the tqdm package, according to https://github.com/tqdm/tqdm/issues/375, a different thing needs to be imported based on whether a progress bar is required on a jupyter notebook [from tqdm import tqdm_notebook as tqdm] or in the terminal [from tqdm import tqdm].

I'm writing a script which makes used of tqdm to display a progress bar, but I don't know whether the user is going to use the script in a Jupyter notebook or a terminal. How can I give it some context awareness so that if the user is calling the script from a jupyter notebook, then it uses tqdm_notebook, and otherwise it uses tqdm.

So, the question is: how can I detect if the environment the script is being called in is a Jupyter notebook.

bluprince13
  • 4,607
  • 12
  • 44
  • 91
  • It seems what you are asking for is already answered in this other [question](https://stackoverflow.com/questions/42212810/tqdm-in-jupyter-notebook) – Carlos Perales Feb 20 '18 at 12:07
  • @CarlosPerales Nope, don't think it does. I know using tqdm_notebook works for Jupyter notebooks. My question isn't about tqdm specifically, it's about how I can detect if the environment is a Jupyter notebook. – bluprince13 Feb 20 '18 at 12:10

1 Answers1

1
import sys
sys.argv
Out[1]:
['/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ipykernel_launcher.py',
 '-f',
 '/Users/patarapolw/Library/Jupyter/runtime/kernel-379556d7-b2ee-4f83-aa33-a8c783c4b4a3.json']

However, if I run the script directly, it would say the filename.

See also, How can I check if code is executed in the IPython notebook?

Polv
  • 1,918
  • 1
  • 20
  • 31