Skip to content

Commit

Permalink
Render nested progress bars in the same container in IPython. Fixes r…
Browse files Browse the repository at this point in the history
…endering issues in VSCode. See: microsoft/vscode-jupyter#9397.
  • Loading branch information
UFO-101 committed Aug 27, 2023
1 parent 4c956c2 commit 24ddfa7
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tqdm/notebook.py
Expand Up @@ -37,22 +37,24 @@
if IPY == 32:
from IPython.html.widgets import HTML
from IPython.html.widgets import FloatProgress as IProgress
from IPython.html.widgets import HBox
from IPython.html.widgets import HBox, VBox
IPY = 3
else:
from ipywidgets import HTML
from ipywidgets import FloatProgress as IProgress
from ipywidgets import HBox
from ipywidgets import HBox, VBox
except ImportError:
try: # IPython 2.x
from IPython.html.widgets import HTML
from IPython.html.widgets import ContainerWidget as HBox
from IPython.html.widgets import ContainerWidget as VBox
from IPython.html.widgets import FloatProgressWidget as IProgress
IPY = 2
except ImportError:
IPY = 0
IProgress = None
HBox = object
VBox = object

try:
from IPython.display import display # , clear_output
Expand Down Expand Up @@ -178,6 +180,9 @@ def display(self, msg=None, pos=None,

# Special signal to close the bar
if close and pbar.bar_style != 'danger': # hide only if no error
# Remove self.container from the list of children of outer_container
self.outer_container.children = tuple(
c for c in self.outer_container.children if c is not self.container)
try:
self.container.close()
except AttributeError:
Expand All @@ -198,6 +203,10 @@ def colour(self, bar_color):
if hasattr(self, 'container'):
self.container.children[-2].style.bar_color = bar_color

@classmethod
def get_first_inst(cls, instance):
return min(cls._instances, key=lambda x: getattr(x, 'pos', float('inf')))

def __init__(self, *args, **kwargs):
"""
Supports the usual `tqdm.tqdm` parameters as well as those listed below.
Expand Down Expand Up @@ -230,11 +239,21 @@ def __init__(self, *args, **kwargs):
# Replace with IPython progress bar display (with correct total)
unit_scale = 1 if self.unit_scale is True else self.unit_scale or 1
total = self.total * unit_scale if self.total else self.total

first_inst = self.get_first_inst(self)
iam_first = first_inst is self
if iam_first:
self.outer_container = VBox()
else:
self.outer_container = first_inst.outer_container

self.container = self.status_printer(self.fp, total, self.desc, self.ncols)
self.outer_container.children += (self.container,)

self.container.pbar = proxy(self)
self.displayed = False
if display_here and self.delay <= 0:
display(self.container)
if display_here and self.delay <= 0 and iam_first:
display(self.outer_container)
self.displayed = True
self.disp = self.display
self.colour = colour
Expand Down

0 comments on commit 24ddfa7

Please sign in to comment.