pythonprogress-barprogressive

multiple python progressive progresssbar with varying lengths


I am trying to use the progressive python progressbar to create two stacked progressbar. It should look something like

Articles[#######            ]
Links   [############]

So if you notice, two progress bars are of differing lengths. I have some code below that creates two progress bars of the same length. I was wondering if someone could tell me how to adjust this so that I can allow each progress bar to be different sizes.

Here is the test code that I developed.

from time import sleep

from blessings import Terminal

from progressive.bar import Bar
from progressive.tree import ProgressTree, Value, BarDescriptor


def progbar(_outer, _inner):

    leaf_values = [Value(0) for i in range(2)]

    test_d = {
    'Link pages scraped':  BarDescriptor(value=leaf_values[0],
                                       type=Bar, max_value = _outer),
    'Articles collected': BarDescriptor(value = leaf_values[1],
                                           type=Bar, max_value= _inner)
    }

    def incr_value(obj, _counter_outer, _counter_inner):
        if _counter_inner < _outer:
            leaf_values[0].value += 1
        if _counter_outer < _inner:
            leaf_values[1].value += 1


    def are_we_done(obj):
        if _counter_inner == _outer and _counter_outer == _inner:
            return(True)
        else:
            return(False)


# Create blessings.Terminal instance
t = Terminal()
# Initialize a ProgressTree instance
n = ProgressTree(term=t)
# We'll use the make_room method to make sure the terminal
#   is filled out with all the room we need
n.make_room(test_d)

_counter_inner = 0
_counter_outer = 0
while not are_we_done(test_d):
    sleep(2)
    n.cursor.restore()
    # We use our incr_value method to bump the fake numbers
    incr_value(test_d,_counter_outer, _counter_inner)
    # Actually draw out the bars
    n.draw(test_d)
    _counter_inner += 1
    _counter_outer += 1


if __name__ == '__main__':

    progbar(100, 20)                             

Solution

  • ok, first I assume the indention problem is from the copy and paste

    to make it in diffrent sizes you need to change the lines

    test_d = {
    'Link pages scraped':  BarDescriptor(value=leaf_values[0],
                                       type=Bar, max_value = _outer),
    'Articles collected': BarDescriptor(value = leaf_values[1],
                                           type=Bar, max_value= _inner)
    }
    

    to:

    test_d = {
    'Link pages scraped':  BarDescriptor(value=leaf_values[0],
                                       type=Bar, kwargs=dict(max_value = _outer,width="50%")),
    'Articles collected': BarDescriptor(value = leaf_values[1],
                                           type=Bar, kwargs=dict(max_value= _inner,width="10%"))
    }
    

    notice that i call the BarDescriptor with kwargs as a normal dict and not with **. thats how they use it in this exapmle: https://github.com/hfaran/progressive/blob/master/progressive/examples.py and it seems to work (the parameters in the kwargs are used to call the Bar class)

    you probably want to change the 10% and 50% to something not hardcoded. the % means percentage of the terminal width. you can also do "20c" witch means it width will be 20 characters