bluprince13
bluprince13

Reputation: 5001

Jupyter notebook context awareness with python

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.

Upvotes: 0

Views: 831

Answers (1)

Polv
Polv

Reputation: 2208

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?

Upvotes: 1

Related Questions