Skip to content

Commit

Permalink
Merge pull request #10755 from mariusvniekerk/progressbar
Browse files Browse the repository at this point in the history
Progressbar
  • Loading branch information
rgbkrk committed Aug 26, 2017
2 parents ea43274 + 6e13394 commit 0a36ff7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
53 changes: 49 additions & 4 deletions IPython/core/display.py
Expand Up @@ -5,7 +5,7 @@
# Distributed under the terms of the Modified BSD License.


from binascii import b2a_hex, b2a_base64
from binascii import b2a_hex, b2a_base64, hexlify
import json
import mimetypes
import os
Expand All @@ -20,9 +20,10 @@
__all__ = ['display', 'display_pretty', 'display_html', 'display_markdown',
'display_svg', 'display_png', 'display_jpeg', 'display_latex', 'display_json',
'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject',
'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'GeoJSON', 'Javascript',
'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close',
'publish_display_data', 'update_display', 'DisplayHandle', 'Video']
'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'ProgressBar', 'JSON',
'GeoJSON', 'Javascript', 'Image', 'clear_output', 'set_matplotlib_formats',
'set_matplotlib_close', 'publish_display_data', 'update_display', 'DisplayHandle',
'Video']

#-----------------------------------------------------------------------------
# utility functions
Expand Down Expand Up @@ -728,6 +729,50 @@ def data(self, svg):
def _repr_svg_(self):
return self._data_and_metadata()

class ProgressBar(DisplayObject):
"""Progressbar supports displaying a progressbar like element
"""
def __init__(self, total):
"""Creates a new progressbar
Parameters
----------
total : int
maximum size of the progressbar
"""
self.total = total
self._progress = 0
self.html_width = '60ex'
self.text_width = 60
self._display_id = hexlify(os.urandom(8)).decode('ascii')

def __repr__(self):
fraction = self.progress / self.total
filled = '=' * int(fraction * self.text_width)
rest = ' ' * (self.text_width - len(filled))
return '[{}{}] {}/{}'.format(
filled, rest,
self.progress, self.total,
)

def _repr_html_(self):
return "<progress style='width:{}' max='{}' value='{}'></progress>".format(
self.html_width, self.total, self.progress)

def display(self):
display(self, display_id=self._display_id)

def update(self):
display(self, display_id=self._display_id, update=True)

@property
def progress(self):
return self._progress

@progress.setter
def progress(self, value):
self._progress = value
self.update()

class JSON(DisplayObject):
"""JSON expects a JSON-able dict or list
Expand Down
7 changes: 7 additions & 0 deletions IPython/core/tests/test_display.py
Expand Up @@ -194,6 +194,13 @@ def test_displayobject_repr():
j._show_mem_addr = False
nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')

def test_progress():
p = display.ProgressBar(10)
nt.assert_true('0/10' in repr(p))
p.html_width = '100%'
p.progress = 5
nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>")

def test_json():
d = {'a': 5}
lis = [d]
Expand Down
10 changes: 10 additions & 0 deletions docs/source/whatsnew/pr/progressbar.rst
@@ -0,0 +1,10 @@
IPython now has built-in support for progressbars::

In[1]: from IPython.display import ProgressBar
... : pb = ProgressBar(100)
... : pb

In[2]: pb.progress = 50

# progress bar in cell 1 updates.

62 changes: 8 additions & 54 deletions examples/IPython Kernel/Updating Displays.ipynb
Expand Up @@ -222,65 +222,24 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<progress\n",
" value=10\n",
" max=10\n",
" style=\"width: 60ex\"/>\n",
" 10 / 10\n",
" "
"<progress style='width:100%' max='10' value='10'></progress>"
],
"text/plain": [
"[============================================================] 10/10"
"<IPython.core.display.ProgressBar object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"from binascii import hexlify\n",
"\n",
"class ProgressBar(object):\n",
" def __init__(self, capacity):\n",
" self.progress = 0\n",
" self.capacity = capacity\n",
" self.html_width = '60ex'\n",
" self.text_width = 60\n",
" self._display_id = hexlify(os.urandom(8)).decode('ascii')\n",
" \n",
" def __repr__(self):\n",
" fraction = self.progress / self.capacity\n",
" filled = '=' * int(fraction * self.text_width)\n",
" rest = ' ' * (self.text_width - len(filled))\n",
" return '[{}{}] {}/{}'.format(\n",
" filled, rest,\n",
" self.progress, self.capacity,\n",
" )\n",
" \n",
" def _repr_html_(self):\n",
" return \"\"\"<progress\n",
" value={progress}\n",
" max={capacity}\n",
" style=\"width: {width}\"/>\n",
" {progress} / {capacity}\n",
" \"\"\".format(\n",
" progress=self.progress,\n",
" capacity=self.capacity,\n",
" width=self.html_width,\n",
" )\n",
" \n",
" def display(self):\n",
" display(self, display_id=self._display_id)\n",
" \n",
" def update(self):\n",
" update_display(self, display_id=self._display_id)\n",
"from IPython.display import ProgressBar\n",
"\n",
"bar = ProgressBar(10)\n",
"bar.display()"
Expand All @@ -295,21 +254,16 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<progress\n",
" value=10\n",
" max=10\n",
" style=\"width: 60ex\"/>\n",
" 10 / 10\n",
" "
"<progress style='width:100%' max='10' value='10'></progress>"
],
"text/plain": [
"[============================================================] 10/10"
"<IPython.core.display.ProgressBar object>"
]
},
"metadata": {},
Expand Down Expand Up @@ -351,7 +305,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 0a36ff7

Please sign in to comment.