8000 magic tqdm · Issue #443 · tqdm/tqdm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

magic tqdm #443

New issue

Have a question about this project? 8000 Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KOLANICH opened this issue Sep 22, 2017 · 10 comments
Closed

magic tqdm #443

KOLANICH opened this issue Sep 22, 2017 · 10 comments
Assignees
Labels
p3-enhancement 🔥 Much new such feature submodule-notebook 📓 Much web such IDE to-merge ↰ Imminent

Comments

@KOLANICH
Copy link
KOLANICH commented Sep 22, 2017
from isNotebook import isNotebook
from tqdm import *

"""Magic calling needed tqdm depending on environment"""

if isNotebook():
	mtqdm=tqdm_notebook
else:
	mtqdm=tqdm

where isNotebook is according to https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook/39662359#39662359

@KOLANICH
Copy link
Author
KOLANICH commented Jan 8, 2018

@casperdcl

@casperdcl
Copy link
Member
casperdcl commented Jan 9, 2018

This is nice but has the additional isNotebook dependency, and the SO link you mention doesn't seem conclusive. Might be better to add to the documentation rather than to the library

@KOLANICH
Copy link
Author
KOLANICH commented Jan 9, 2018

We have to reimplement IsNotebook. It's pretty tiny.

@casperdcl
Copy link
Member

yes but my concern is there doesn't seem to be a consensus on how precisely to write an isNotebook that actually works in all cases

@KOLANICH
Copy link
Author
KOLANICH commented Jan 9, 2018

That one works very well, as intended.

@joshbode
Copy link
joshbode commented Feb 28, 2018

I've been using the following in my library, where I only want the progress bars if I'm running in an IPython environment:

if 'IPython' in sys.modules:

    from IPython import get_ipython

    if 'IPKernelApp' in get_ipython().config:
        from tqdm import tqdm_notebook as tqdm
    else:
        from tqdm import tqdm

else:

    def tqdm(x, **kwargs):
        """Fake progress function."""

        return x

Not sure how robust it is, though, but it correctly distinguishes between terminal ipython and Jupyter Notebook/Lab. It does not correctly identify jupyter console though, incorrectly using tqdm_notebook

@joshbode
Copy link
joshbode commented Mar 1, 2018

Something like this could possibly work, which correctly returns False for ipython, jupyter console and jupyter qtconsole:

import re
import psutil

def is_notebook():
    return any(
        re.search(r'\bjupyter-(lab|notebook)\b', x)
        for x in psutil.Process().parent().cmdline()
    )

I'm not sure if it works for remote kernels, though

@chengs
Copy link
Contributor
chengs commented Apr 26, 2018

maybe checking ipywidget instead of jupyter?

@casperdcl
Copy link
Member

Basically, we can detect:

  1. ipython/python
  2. jupyter console/notebook

the problem being that console is not distinguishable from notebook by design:

I'd be in favour of:

from tqdm.magic import tqdm

which would:

  • if in plain terminal/ipython: return tqdm
  • if in jupyter, actually return tqdm_notebook but warn that jupyter console won't work properly

casperdcl added a commit that referenced this issue May 31, 2018
casperdcl added a commit that referenced this issue Jun 3, 2018
@casperdcl casperdcl added p3-enhancement 🔥 Much new such feature to-merge ↰ Imminent submodule-notebook 📓 Much web such IDE labels Jun 3, 2018
@casperdcl casperdcl self-assigned this Jun 3, 2018
@KOLANICH
Copy link
Author

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-enhancement 🔥 Much new such feature submodule-notebook 📓 Much web such IDE to-merge ↰ Imminent
Projects
None yet
Development

No branches or pull requests

4 participants
0