-
Notifications
You must be signed in to change notification settings - Fork 4
★ 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
Comments
See this answer in StackOverflow: https://stackoverflow.com/a/63553373/1603480 If you have
|
... 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' |
This is an example on how to do, with the tests (which are more complicated than the feature itself).
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! |
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) |
Added some code for jupyter check in #80 |
★ Enviroment sensitive progress bar
Is your feature request related to a problem? Please describe.
For now the progress bar we used is for notebook
unpackai/unpackai/utils.py
Line 21 in 640a24f
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 jupyterThen we can import progressbar correctly
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 valueThe text was updated successfully, but these errors were encountered: