多个 python 不同长度的渐进式进度条
multiple python progressive progresssbar with varying lengths
我正在尝试使用 progressive
python 进度条来创建两个堆叠的进度条。它应该看起来像
Articles[####### ]
Links [############]
因此,如果您注意到,两个进度条的长度不同。我在下面有一些代码可以创建两个相同长度的进度条。我想知道是否有人可以告诉我如何调整它以便我可以让每个进度条的大小不同。
下面是我开发的测试代码
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)
好的,首先我假设缩进问题来自复制和粘贴
要使其具有不同的尺寸,您需要更改线条
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)
}
至:
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%"))
}
请注意,我将带有 kwargs 的 BarDescriptor 称为普通字典,而不是带有 **。这就是他们在这个例子中使用它的方式:https://github.com/hfaran/progressive/blob/master/progressive/examples.py
它似乎有效(kwargs 中的参数用于调用 Bar class)
您可能想将 10% 和 50% 更改为未硬编码的值。
% 表示终端宽度的百分比。你也可以做“20c” 意味着它的宽度将是 20 个字符
我正在尝试使用 progressive
python 进度条来创建两个堆叠的进度条。它应该看起来像
Articles[####### ]
Links [############]
因此,如果您注意到,两个进度条的长度不同。我在下面有一些代码可以创建两个相同长度的进度条。我想知道是否有人可以告诉我如何调整它以便我可以让每个进度条的大小不同。
下面是我开发的测试代码
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)
好的,首先我假设缩进问题来自复制和粘贴
要使其具有不同的尺寸,您需要更改线条
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)
}
至:
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%"))
}
请注意,我将带有 kwargs 的 BarDescriptor 称为普通字典,而不是带有 **。这就是他们在这个例子中使用它的方式:https://github.com/hfaran/progressive/blob/master/progressive/examples.py 它似乎有效(kwargs 中的参数用于调用 Bar class)
您可能想将 10% 和 50% 更改为未硬编码的值。 % 表示终端宽度的百分比。你也可以做“20c” 意味着它的宽度将是 20 个字符