A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/ipython/ipython/issues/1527/ below:

Making a progress bar work in IPython Notebook · Issue #1527 · ipython/ipython · GitHub

I have modified a simple progressbar that I was hoping would work with IPy notebooks. It works fine in a standard console:

however, in a notebook it does not:

Here are the guts of the progressbar code:

import sys, time
try:
    import IPython
    clear_output = IPython.core.display.clear_output
except:
    IPython = None

class ProgressBar:
    def __init__(self, iterations):
        self.iterations = iterations
        self.prog_bar = '[]'
        self.fill_char = '*'
        self.width = 40
        self.__update_amount(0)
        if IPython:
            self.animate = self.animate_ipython
        else:
            self.animate = self.animate_noipython

    def animate_ipython(self, iter):
        if sys.platform.lower().startswith('win'):
            print self, '\r',
        else:

            print self, chr(27),
        self.update_iteration(iter + 1)
        # time.sleep(0.5)
        clear_output()

    def update_iteration(self, elapsed_iter):
        self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)
        self.prog_bar += '  %d of %s complete' % (elapsed_iter, self.iterations)

    def __update_amount(self, new_amount):
        percent_done = int(round((new_amount / 100.0) * 100.0))
        all_full = self.width - 2
        num_hashes = int(round((percent_done / 100.0) * all_full))
        self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
        pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
        pct_string = '%d%%' % percent_done
        self.prog_bar = self.prog_bar[0:pct_place] + \
            (pct_string + self.prog_bar[pct_place + len(pct_string):])

    def __str__(self):
        return str(self.prog_bar)

So, at every iteration of my code (an MCMC algorithm), the sampler calls animate(tier) to update the progress bar with the current iteration. I am using IPython.core.display.clear_output to clear the cell output at each iteration, but for some reason ti does not work here. Any help would be appreciated. I would love to use PyMC in IPython Notebooks!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4