Skip to content

Commit 8789786

Browse files
committed
Fixes issue AnswerDotAI#69 Visual Studio Code does not render progress bars correctly
1 parent 1a1692a commit 8789786

File tree

3 files changed

+149
-114
lines changed

3 files changed

+149
-114
lines changed

fastprogress/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"IN_NOTEBOOK": "00_core.ipynb",
1212
"ProgressBar": "01_fastprogress.ipynb",
1313
"MasterBar": "01_fastprogress.ipynb",
14+
"NBOutput": "01_fastprogress.ipynb",
1415
"NBProgressBar": "01_fastprogress.ipynb",
1516
"NBMasterBar": "01_fastprogress.ipynb",
1617
"NO_BAR": "01_fastprogress.ipynb",

fastprogress/fastprogress.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/01_fastprogress.ipynb (unless otherwise specified).
22

3-
__all__ = ['ProgressBar', 'MasterBar', 'NBProgressBar', 'NBMasterBar', 'NO_BAR', 'WRITER_FN', 'FLUSH', 'SAVE_PATH',
4-
'SAVE_APPEND', 'MAX_COLS', 'printing', 'ConsoleProgressBar', 'print_and_maybe_save', 'ConsoleMasterBar',
5-
'master_bar', 'progress_bar', 'force_console_behavior', 'workaround_empty_console_output']
3+
__all__ = ['ProgressBar', 'MasterBar', 'NBOutput', 'NBProgressBar', 'NBMasterBar', 'NO_BAR', 'WRITER_FN', 'FLUSH',
4+
'SAVE_PATH', 'SAVE_APPEND', 'MAX_COLS', 'printing', 'ConsoleProgressBar', 'print_and_maybe_save',
5+
'ConsoleMasterBar', 'master_bar', 'progress_bar', 'force_console_behavior',
6+
'workaround_empty_console_output']
67

78
# Cell
89
import time,os,shutil
@@ -98,16 +99,30 @@ def update(self, val): self.main_bar.update(val)
9899
try:
99100
from IPython.display import clear_output, display, HTML
100101
import matplotlib.pyplot as plt
102+
import ipywidgets as widgets
101103
except:
102104
warn("Couldn't import ipywidgets properly, progress bar will use console behavior")
103105
IN_NOTEBOOK = False
104106

107+
# Cell
108+
class NBOutput():
109+
def __init__(self, to_display):
110+
self.out = widgets.Output()
111+
display(self.out)
112+
with self.out:
113+
display(to_display)
114+
115+
def update(self, to_update):
116+
with self.out:
117+
clear_output(wait=True)
118+
display(to_update)
119+
105120
# Cell
106121
class NBProgressBar(ProgressBar):
107122
def on_iter_begin(self):
108123
super().on_iter_begin()
109124
self.progress = html_progress_bar(0, self.total, "")
110-
if self.display: self.out = display(HTML(self.progress), display_id=True)
125+
if self.display: self.out = NBOutput(HTML(self.progress))
111126
self.is_active=True
112127

113128
def on_interrupt(self):
@@ -138,7 +153,7 @@ def __init__(self, gen, total=None, hide_graph=False, order=None, clean_on_inter
138153

139154
def on_iter_begin(self):
140155
self.html_code = '\n'.join([html_progress_bar(0, self.main_bar.total, ""), ""])
141-
self.out = display(HTML(self.html_code), display_id=True)
156+
self.out = NBOutput(HTML(self.html_code))
142157

143158
def on_interrupt(self):
144159
if self.clean_on_interrupt: self.out.update(HTML(''))
@@ -182,14 +197,14 @@ def show_imgs(self, imgs, titles=None, cols=4, imgsize=4, figsize=None):
182197
if titles is None: titles = [None] * len(imgs)
183198
for img, ax, title in zip(imgs, imgs_axs.flatten(), titles): img.show(ax=ax, title=title)
184199
for ax in imgs_axs.flatten()[len(imgs):]: ax.axis('off')
185-
if not hasattr(self, 'imgs_out'): self.imgs_out = display(self.imgs_fig, display_id=True)
200+
if not hasattr(self, 'imgs_out'): self.imgs_out = NBOutput(self.imgs_fig)
186201
else: self.imgs_out.update(self.imgs_fig)
187202

188203
def update_graph(self, graphs, x_bounds=None, y_bounds=None, figsize=(6,4)):
189204
if self.hide_graph: return
190205
if not hasattr(self, 'graph_fig'):
191206
self.graph_fig, self.graph_ax = plt.subplots(1, figsize=figsize)
192-
self.graph_out = display(self.graph_ax.figure, display_id=True)
207+
self.graph_out = NBOutput(self.graph_ax.figure)
193208
self.graph_ax.clear()
194209
if len(self.names) < len(graphs): self.names += [''] * (len(graphs) - len(self.names))
195210
for g,n in zip(graphs,self.names): self.graph_ax.plot(*g, label=n)

0 commit comments

Comments
 (0)