diff --git a/fastprogress/_nbdev.py b/fastprogress/_nbdev.py index dd58d0c..05a0d27 100644 --- a/fastprogress/_nbdev.py +++ b/fastprogress/_nbdev.py @@ -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", diff --git a/fastprogress/fastprogress.py b/fastprogress/fastprogress.py index b01f0a5..ac77522 100644 --- a/fastprogress/fastprogress.py +++ b/fastprogress/fastprogress.py @@ -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 @@ -98,10 +99,24 @@ 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): @@ -109,7 +124,7 @@ def on_iter_begin(self): 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): @@ -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('')) @@ -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) @@ -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 = '' \ No newline at end of file diff --git a/nbs/01_fastprogress.ipynb b/nbs/01_fastprogress.ipynb index 4e6d48c..e7c89ac 100644 --- a/nbs/01_fastprogress.ipynb +++ b/nbs/01_fastprogress.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -103,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -140,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -156,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -181,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -195,8 +195,10 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 10, + "metadata": { + "tags": [] + }, "outputs": [ { "name": "stdout", @@ -223,7 +225,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -241,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -250,6 +252,7 @@ " try:\n", " from IPython.display import clear_output, display, HTML\n", " import matplotlib.pyplot as plt\n", + " import ipywidgets as widgets\n", " except:\n", " warn(\"Couldn't import ipywidgets properly, progress bar will use console behavior\")\n", " IN_NOTEBOOK = False" @@ -257,7 +260,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "#export\n", + "class NBOutput():\n", + " def __init__(self, to_display):\n", + " self.out = widgets.Output()\n", + " display(self.out)\n", + " with self.out:\n", + " display(to_display)\n", + "\n", + " def update(self, to_update):\n", + " with self.out:\n", + " clear_output(wait=True)\n", + " display(to_update)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -268,7 +291,7 @@ " self.progress = html_progress_bar(0, self.total, \"\")\n", " if self.display:\n", " display(HTML(html_progress_bar_styles))\n", - " self.out = display(HTML(self.progress), display_id=True)\n", + " self.out = NBOutput(HTML(self.progress))\n", " self.is_active=True\n", "\n", " def on_interrupt(self):\n", @@ -289,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -318,16 +341,13 @@ }, { "data": { - "text/html": [ - "\n", - "
Finished loop 1.
Finished loop 2.
Finished loop 3.
Finished loop 4."
- ],
+ "application/vnd.jupyter.widget-view+json": {
+ "model_id": "f43e12da2bd94df1837ca025ae953f00",
+ "version_major": 2,
+ "version_minor": 0
+ },
"text/plain": [
- " Finished loop 1. Finished loop 2. Finished loop 3. Finished loop 4."
- ],
+ "application/vnd.jupyter.widget-view+json": {
+ "model_id": "9687ec9d143e4c6ea5b67355e9ba5a10",
+ "version_major": 2,
+ "version_minor": 0
+ },
"text/plain": [
- "