Last active
March 10, 2018 21:06
-
-
Save JossWhittle/43e1ae50e4c7dbb86b052dcce8ec7bc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from IPython.lib.backgroundjobs import BackgroundJobManager | |
| from IPython.core.magic import register_line_magic | |
| from IPython.display import display | |
| from IPython import get_ipython | |
| from itertools import islice, chain | |
| from ipywidgets import IntProgress, HTML, VBox | |
| # Iterate through a generator without peaking, yeilding a list of elements | |
| # | |
| def chunks(iterable, size): | |
| iterator = iter(iterable) | |
| for first in iterator: | |
| yield list(chain([first], islice(iterator, (len(iterable)//size) - 1))) | |
| # Magics for interacting with jobs | |
| # | |
| def jobs_manager(): | |
| jobs = BackgroundJobManager() | |
| @register_line_magic | |
| def job(line): | |
| ip = get_ipython() | |
| jobs.new(line, ip.user_global_ns) | |
| return jobs | |
| # Pretty progress bars for for jupyter notebook | |
| # | |
| def log_progress(sequence, every=None, size=None, name='Jobs'): | |
| is_iterator = False | |
| if size is None: | |
| try: | |
| size = len(sequence) | |
| except TypeError: | |
| is_iterator = True | |
| if size is not None: | |
| if every is None: | |
| if size <= 200: | |
| every = 1 | |
| else: | |
| every = int(size / 200) # every 0.5% | |
| else: | |
| assert every is not None, 'sequence is iterator, set every' | |
| if is_iterator: | |
| progress = IntProgress(min=0, max=1, value=1) | |
| progress.bar_style = 'info' | |
| else: | |
| progress = IntProgress(min=0, max=size, value=0) | |
| label = HTML() | |
| box = VBox(children=[label, progress]) | |
| display(box) | |
| index = 0 | |
| try: | |
| for index, record in enumerate(sequence, 1): | |
| if index == 1 or index % every == 0: | |
| if is_iterator: | |
| label.value = '{name}: {index} / ?'.format( | |
| name=name, | |
| index=index | |
| ) | |
| else: | |
| progress.value = index | |
| label.value = u'{name}: {index} / {size}'.format( | |
| name=name, | |
| index=index, | |
| size=size | |
| ) | |
| yield record | |
| except: | |
| progress.bar_style = 'danger' | |
| raise | |
| else: | |
| progress.bar_style = 'success' | |
| progress.value = index | |
| label.value = "{name}: {index}".format( | |
| name=name, | |
| index=str(index or '?') | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment