Skip to content

★ Enviroment sensitive progress bar #47

New issue

Have a question about this project? 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

Open
raynardj opened this issue Oct 19, 2021 · 5 comments
Open

★ Enviroment sensitive progress bar #47

raynardj opened this issue Oct 19, 2021 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@raynardj
Copy link
Collaborator

raynardj commented Oct 19, 2021

★ Enviroment sensitive progress bar

Is your feature request related to a problem? Please describe.
For now the progress bar we used is for notebook

from tqdm.notebook import tqdm

If we use this in non-frontend situation, this can break.

Describe the solution you'd like
create a function def is_jupyter() -> bool: to return is this environment jupyter

Then we can import progressbar correctly

if  is_jupyter():
    from tqdm.notebook import tqdm
else:
    from tqdm import tqdm

Additional context
of course, mostly, our code will run in jupyter, so this issue isn't very pressing

is_jupyter(), I've tried on other projects. This function is harder than I thought, in many cases it can return wrong value

@raynardj raynardj added enhancement New feature or request good first issue Good for newcomers labels Oct 19, 2021
@jfthuong
Copy link
Contributor

See this answer in StackOverflow: https://stackoverflow.com/a/63553373/1603480

If you have tqdm >= 4.27.0, you can import from tqdm.auto to handle this:

from tqdm.auto import tqdm

tqdm can then be used as normal. If you are in a notebook, tqdm will be imported from .notebook.

@jfthuong
Copy link
Contributor

... or this code:

def type_of_interpreter() -> str:
    """Return what type of interpreter we have: terminal, ipython, or jupyter"""
    try:
        ipy_str = str(type(get_ipython()))
        if 'zmqshell' in ipy_str:
            return 'jupyter'
        if 'terminal' in ipy_str:
            return 'ipython'
    except Exception:
        pass
    return 'terminal'

@jfthuong
Copy link
Contributor

This is an example on how to do, with the tests (which are more complicated than the feature itself).

  1. We need nbmake module
  2. We need to run pytest with the option --nbmake: pytest --nbmake --cov ...

tqdm_jupyter.zip

Need to spend time to add in an official notebook, update the list of modules requirements for test (requirements-test.txt), and update the github action to run also the tests in the notebooks.

A good task to start learning about the module!

@jfthuong
Copy link
Contributor

FYI:

CODE (in_jupyter.py but should definitely be renamed)

def type_of_interpreter() -> str:
    """Return what type of interpreter we have: terminal, ipython, or jupyter"""
    try:
        ipy_str = str(type(get_ipython()))  # type:ignore  # catch error
        return "jupyter" if "terminal" not in ipy_str else "ipython"
    except Exception:
        return "terminal"


def in_jupyter() -> bool:
    """Check if the code is run inside Jupyter"""
    try:
        return "zmqshell" in str(type(get_ipython()))  # type:ignore  # catch error
    except Exception:
        return False


def get_tqdm():
    """Load the good tqdm based on whether it is Jupyter"""
    if in_jupyter():
        from tqdm.notebook import tqdm
    else:
        from tqdm import tqdm

    return tqdm


tqdm = get_tqdm()

TEST

import in_jupyter
import tqdm as tqdm_module
from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.core.interactiveshell import InteractiveShell


def mockup_get_ipython():
    return TerminalInteractiveShell()


def test_in_jupyter():
    """Test if in Jupyter"""
    assert not in_jupyter.in_jupyter(), "Detected that in jupyter"


def test_in_jupyter_ipython(monkeypatch):
    monkeypatch.setitem(globals(), "get_ipython", mockup_get_ipython)
    assert not in_jupyter.in_jupyter(), "Detected that in jupyter"


def test_type_of_interpreter_terminal():
    assert in_jupyter.type_of_interpreter() == "terminal"


def test_type_of_interpreter_ipython(monkeypatch):
    in_jupyter.get_ipython = None
    monkeypatch.setattr(in_jupyter, "get_ipython", mockup_get_ipython)
    assert in_jupyter.type_of_interpreter() == "ipython"


def test_tqdm():
    assert in_jupyter.get_tqdm() is tqdm_module.std.tqdm

... and there shall be a Notebook with few tests

JUPYTER

from in_jupyter import in_jupyter, type_of_interpreter, tqdm
import tqdm as tqdm_module

# New Cell

def test_in_jupyter():
    """Test if in Jupyter"""
    assert in_jupyter(), "Not detected that in jupyter"

def test_type_of_interpreter():
    """Test if Jupyter, IPython, or Terminal"""
    assert type_of_interpreter() == "jupyter"

def test_tqdm():
    assert tqdm is tqdm_module.notebook.tqdm_notebook

# New Cell

from time import sleep
for i in tqdm(range(100)):
    sleep(0.005)

@jfthuong
Copy link
Contributor

Added some code for jupyter check in #80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants