Skip to content

Commit

Permalink
Fixes issue fastai#69 Visual Studio Code does not render progress bar…
Browse files Browse the repository at this point in the history
…s correctly
  • Loading branch information
hsm committed Sep 4, 2020
1 parent 1a1692a commit 8789786
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 114 deletions.
1 change: 1 addition & 0 deletions fastprogress/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"IN_NOTEBOOK": "00_core.ipynb",
"ProgressBar": "01_fastprogress.ipynb",
"MasterBar": "01_fastprogress.ipynb",
"NBOutput": "01_fastprogress.ipynb",
"NBProgressBar": "01_fastprogress.ipynb",
"NBMasterBar": "01_fastprogress.ipynb",
"NO_BAR": "01_fastprogress.ipynb",
Expand Down
29 changes: 22 additions & 7 deletions fastprogress/fastprogress.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/01_fastprogress.ipynb (unless otherwise specified).

__all__ = ['ProgressBar', 'MasterBar', 'NBProgressBar', 'NBMasterBar', 'NO_BAR', 'WRITER_FN', 'FLUSH', 'SAVE_PATH',
'SAVE_APPEND', 'MAX_COLS', 'printing', 'ConsoleProgressBar', 'print_and_maybe_save', 'ConsoleMasterBar',
'master_bar', 'progress_bar', 'force_console_behavior', 'workaround_empty_console_output']
__all__ = ['ProgressBar', 'MasterBar', 'NBOutput', 'NBProgressBar', 'NBMasterBar', 'NO_BAR', 'WRITER_FN', 'FLUSH',
'SAVE_PATH', 'SAVE_APPEND', 'MAX_COLS', 'printing', 'ConsoleProgressBar', 'print_and_maybe_save',
'ConsoleMasterBar', 'master_bar', 'progress_bar', 'force_console_behavior',
'workaround_empty_console_output']

# Cell
import time,os,shutil
Expand Down Expand Up @@ -98,16 +99,30 @@ def update(self, val): self.main_bar.update(val)
try:
from IPython.display import clear_output, display, HTML
import matplotlib.pyplot as plt
import ipywidgets as widgets
except:
warn("Couldn't import ipywidgets properly, progress bar will use console behavior")
IN_NOTEBOOK = False

# Cell
class NBOutput():
def __init__(self, to_display):
self.out = widgets.Output()
display(self.out)
with self.out:
display(to_display)

def update(self, to_update):
with self.out:
clear_output(wait=True)
display(to_update)

# Cell
class NBProgressBar(ProgressBar):
def on_iter_begin(self):
super().on_iter_begin()
self.progress = html_progress_bar(0, self.total, "")
if self.display: self.out = display(HTML(self.progress), display_id=True)
if self.display: self.out = NBOutput(HTML(self.progress))
self.is_active=True

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

def on_iter_begin(self):
self.html_code = '\n'.join([html_progress_bar(0, self.main_bar.total, ""), ""])
self.out = display(HTML(self.html_code), display_id=True)
self.out = NBOutput(HTML(self.html_code))

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

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

0 comments on commit 8789786

Please sign in to comment.