I'm looking for a reliable way to figure out if my module is being loaded/run from within a Jupyter notebook, or more specifically, if ipywidgets
is available.
This is not a duplicate of other questions: everything else I've found either has no reliable solution, or (more often) they use the "just try it and fail gently" approach that's common in Python. In my case, I'm trying to write the following logic:
if in_jupyter():
from tqdm import tqdm_notebook as tqdm
else:
from tqdm import tqdm
I don't think "try and fail" is an appropriate solution here since I don't want to produce any output yet.
The closest I've found to a solution so far is:
from IPython import get_ipython
get_ipython().config['IPKernelApp']['parent_appname'] == 'ipython-notebook'
but this configuration property is some seemingly empty traitlets.config.loader.LazyConfigValue
(.get_value(None)
is just an empty string).