From 5fb804b6aabbe4ac718c05aa145d638a813c8e11 Mon Sep 17 00:00:00 2001 From: Matthew Wardrop Date: Fri, 24 Mar 2017 12:19:29 -0700 Subject: [PATCH 1/2] Check for Jupyter Notebook before allocating other terminal sizes. Currently, the number of columns and rows used by `progressbar2` in Jupyter notebooks is inherited from the shell from which it was run... which leads to some pretty weird behaviour. Instead, I think we should check for Jupyter notebooks first. Is there ever a case where this doesn't make sense? --- progressbar/utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/progressbar/utils.py b/progressbar/utils.py index 3bbd1519..0f3d9328 100644 --- a/progressbar/utils.py +++ b/progressbar/utils.py @@ -106,6 +106,16 @@ def get_terminal_size(): # pragma: no cover Returns: width, height: Two integers containing width and height ''' + + try: + # Default to 79 characters for IPython notebooks + ipython = globals().get('get_ipython')() + from ipykernel import zmqshell + if isinstance(ipython, zmqshell.ZMQInteractiveShell): + return 79, 24 + except Exception: # pragma: no cover + pass + try: # This works for Python 3, but not Pypy3. Probably the best method if # it's supported so let's always try @@ -136,15 +146,6 @@ def get_terminal_size(): # pragma: no cover except Exception: # pragma: no cover pass - try: - # Default to 79 characters for IPython notebooks - ipython = globals().get('get_ipython')() - from ipykernel import zmqshell - if isinstance(ipython, zmqshell.ZMQInteractiveShell): - return 79, 24 - except Exception: # pragma: no cover - pass - try: w, h = _get_terminal_size_linux() if w and h: From fc7ab7bda7ef3b5763cbda33598a4352e23996a7 Mon Sep 17 00:00:00 2001 From: Matthew Wardrop Date: Fri, 24 Mar 2017 12:44:45 -0700 Subject: [PATCH 2/2] Fix PEP8 issues. --- progressbar/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/progressbar/utils.py b/progressbar/utils.py index 0f3d9328..6cd64aeb 100644 --- a/progressbar/utils.py +++ b/progressbar/utils.py @@ -106,7 +106,7 @@ def get_terminal_size(): # pragma: no cover Returns: width, height: Two integers containing width and height ''' - + try: # Default to 79 characters for IPython notebooks ipython = globals().get('get_ipython')() @@ -115,7 +115,7 @@ def get_terminal_size(): # pragma: no cover return 79, 24 except Exception: # pragma: no cover pass - + try: # This works for Python 3, but not Pypy3. Probably the best method if # it's supported so let's always try