Skip to content

Fixes issue #69 Visual Studio Code does not render progress bars corr… #70

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastprogress/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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
31 changes: 23 additions & 8 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,18 +99,32 @@ 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:
display(HTML(html_progress_bar_styles))
self.out = display(HTML(self.progress), display_id=True)
self.out = NBOutput(HTML(self.progress))
self.is_active=True

def on_interrupt(self):
Expand Down Expand Up @@ -141,7 +156,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, ""), ""])
display(HTML(html_progress_bar_styles))
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 @@ -185,14 +200,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 Expand Up @@ -305,4 +320,4 @@ def force_console_behavior():
# Cell
def workaround_empty_console_output():
"Change console output behaviour to correctly show progress in consoles not recognizing \r at the end of line"
ConsoleProgressBar.end = ''
ConsoleProgressBar.end = ''
Loading