Open aebrahim opened 9 years ago
I had this problem too, and solved it using display_javascript. While I haven't gotten it working for IProgress (because checking is all done at import time in progressbar.py file), I do have it working for gaulinmp/tqdm.
Basically in the import preamble of the module, I put the following:
display.display_javascript("""IPython.notebook.kernel.execute(
'from IPython import get_ipython;
get_ipython().has_js=True');""", raw=True)
Then I have a simple check function which is run whenever I want to know if I'm in a notebook:
ip = IPython.get_ipython()
...
def _has_js():
return hasattr(ip, 'has_js')
Because the get_ipython() object is global (seems to be in testing at least, I'm ignorant here on the specifics), the _has_js call will find the has_js object only if javascript is present, which is only the case when running a notebook. I don't know if this is always true though, so YMMV.
I didn't get it running for IProgress, but I think that's because the progressbar.py file checks for IPython at import time, not when the progress bar object is created. That's why I defer the check to decide which type of progress bar to create in my fork of tqdm linked above. I did copy the rest of your import code though, thanks for that!
As of 309209e, this does not work with qtconsole.
The issue is that there is no way to differentiate between the notebook (which can display a widget), and the qt console (which can not). When the notebook is detected, only a widget is displayed, and the text output is suppressed.