Skip to content

Commit

Permalink
✨ progress bar using ipyvuetify for in the jupyter notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Dec 6, 2021
1 parent 61173c8 commit f9e2f43
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
110 changes: 109 additions & 1 deletion packages/vaex-core/vaex/misc/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import sys
import threading
import time
import multiprocessing
import vaex.utils

cpu_count = multiprocessing.cpu_count()


class ProgressBarBase(object):
def __init__(self, min_value, max_value, title='vaex', format="%(percentage) 6.2f%% %(timeinfo)s cpu: %(cpu_usage)d%%"):
Expand Down Expand Up @@ -57,7 +60,7 @@ def info(self):
timeinfo = "estimated time: % 8.2fs = % 4.1fm = % 2.1fh" % (seconds, minutes, hours)
else:
timeinfo = "estimated time: unknown "
return {"percentage":percentage, "timeinfo":timeinfo, "cpu_usage": cpu_usage, "title": self.title}
return {"percentage":percentage, "timeinfo":timeinfo, "cpu_usage": cpu_usage, "title": self.title, "cpu_count": cpu_count}

def __repr__(self):
output = ''
Expand Down Expand Up @@ -196,6 +199,7 @@ def __rich_console__(self, console, options):



# TODO: make this configutable with new settings system
show_extra = vaex.utils.get_env_type(bool, 'VAEX_PROGRESS_EXTRA', False)

class ProgressBarRich(ProgressBarBase):
Expand Down Expand Up @@ -267,4 +271,108 @@ def update(self, value):
def finish(self):
self(self.max_value)
if self.parent is None:
if show_extra:
self.info.stop()
self.live.stop()


import ipyvuetify as v
import traitlets
class ProgressDashboard(v.VuetifyTemplate):
items = traitlets.Any({}).tag(sync=True)
extra = traitlets.Any({}).tag(sync=True)
selected = traitlets.Unicode(default_value=None, allow_none=True).tag(sync=True)
@traitlets.default('template')
def _template(self):
return '''
<template>
<div>
<pre>{{extra.memory}}
{{extra.disk}}
{{extra.net}}</pre>
<v-treeview open-all dense :items="items">
<template v-slot:prepend="{ item, open }">
<v-progress-circular v-if='!item.finished' rotate="-90" :indeterminate="!item.started" :key="item.percentage" :size="30" :width="5" :value="item.percentage" :color="item.percentage < 100 ? 'primary' : 'green'">{{ item.started ? item.percentage : '' }}</v-progress-circular>
<v-icon color="green" v-if='item.finished'>mdi-checkbox-marked-circle</v-icon>
</template>
<template v-slot:label="{ item, open }">
{{item.title}} | {{item.timeinfo}} |
<span :class="item.cpu_usage / item.cpu_count < 33 ? 'vaex-cpu-low' : (item.cpu_usage / item.cpu_count < 66 ? 'vaex-cpu-medium' : 'vaex-cpu-high')">{{Math.ceil(item.cpu_usage)}}% cpu</span>
<!--
<v-progress-linear v-model="item.cpu_usage / item.cpu_count" color="blue-grey" height="25" style="width: 100px">
<template v-slot:default="{ value }">
<strong>cpu: {{ Math.ceil(value) }}%</strong>
</template>
</v-progress-linear>
-->
</template>
</v-treeview>
</div>
</template>
<style id="vaex-progress-dashboard-style">
.vaex-cpu-low {
color: red;
}
.vaex-cpu-medium {
color: orange;
}
.vaex-cpu-high {
color: green;
}
</style>
'''

class ProgressBarVuetify(ProgressBarBase):
def __init__(self, min_value, max_value, title=None, progress=None, indent=0, parent=None):
if len(title) > 60:
title = title[:60-3] + "..."
super(ProgressBarVuetify, self).__init__(min_value, max_value, title=title)
self.parent = parent
self.data = {'children': [], 'name': self.title, 'started': False, 'finished': False}
self.data.update(self.info())
self.parent = parent
if parent is None:
self.psutil = Psutil(daemon=True)
self.psutil.start()
self.data
self.dashboard = ProgressDashboard(items=[self.data])
from IPython.display import display
display(self.dashboard)
else:
self.dashboard = None
self.steps = 0
self.indent = indent


def add_child(self, parent, task, title):
child = ProgressBarVuetify(self.min_value, self.max_value, title, indent=self.indent+1, parent=self)
self.data['children'].append(child.data)
return child

def __call__(self, value):
if self.value == 0:
self.info() # initialize
self.value = value
self.update_fraction()
info = self.info()
info['percentage'] = int(info['percentage'])
info['started'] = True
info['finished'] = self.value == self.max_value
self.data.update(info)
if self.parent is None:
self.dashboard.extra = {
'memory': self.psutil.memory,
'disk': self.psutil.disk,
'net': self.psutil.net,
}
self.dashboard.send_state('items')

def update(self, value):
self(value)

def finish(self):
self(self.max_value)
if self.parent is None:
self.psutil.stop()
5 changes: 5 additions & 0 deletions packages/vaex-core/vaex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,17 @@ def _progressbar_rich(type=None, title="processing", max_value=1):
import vaex.misc.progressbar as pb
return pb.ProgressBarRich(0, 1, title=title)

def _progressbar_vuetify(type=None, title="processing", max_value=1):
import vaex.misc.progressbar as pb
return pb.ProgressBarVuetify(0, 1, title=title)


_progressbar_typemap = {}
_progressbar_typemap['progressbar2'] = _progressbar_progressbar2
_progressbar_typemap['vaex'] = _progressbar_vaex
_progressbar_typemap['widget'] = _progressbar_widget
_progressbar_typemap['rich'] = _progressbar_rich
_progressbar_typemap['vuetify'] = _progressbar_vuetify


def progressbar(type_name=None, title="processing", max_value=1):
Expand Down

0 comments on commit f9e2f43

Please sign in to comment.