Skip to content

Commit

Permalink
testing multibar examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick van Hattem authored and Rick van Hattem committed Jan 2, 2024
1 parent 2a1679a commit 7dff419
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import random
import sys
import threading
import time
import typing

Expand Down Expand Up @@ -50,6 +51,68 @@ def prefixed_shortcut_example():
time.sleep(0.1)


@example
def parallel_bars_multibar_example():
BARS = 5
N = 50

def do_something(bar):
for i in bar(range(N)):
# Sleep up to 0.1 seconds
time.sleep(random.random() * 0.1)

with (progressbar.MultiBar() as multibar):
bar_labels = []
for i in range(BARS):
# Get a progressbar
bar_label = 'Bar #%d' % i
bar_labels.append(bar_label)
bar = multibar[bar_label]

for i in range(N * BARS):

time.sleep(0.005)

bar_i = random.randrange(0, BARS)
bar_label = bar_labels[bar_i]
# Increment one of the progress bars at random
multibar[bar_label].increment()

@example
def multiple_bars_line_offset_example():
BARS = 5
N = 100

# Construct the list of progress bars with the `line_offset` so they draw
# below each other
bars = []
for i in range(BARS):
bars.append(
progressbar.ProgressBar(
max_value=N,
# We add 1 to the line offset to account for the `print_fd`
line_offset=i + 1,
max_error=False,
)
)

# Create a file descriptor for regular printing as well
print_fd = progressbar.LineOffsetStreamWrapper(lines=0, stream=sys.stdout)

# The progress bar updates, normally you would do something useful here
for i in range(N * BARS):
time.sleep(0.005)

# Increment one of the progress bars at random
bars[random.randrange(0, BARS)].increment()

# Cleanup the bars
for bar in bars:
bar.finish()
# Add a newline to make sure the next print starts on a new line
print()


@example
def templated_shortcut_example():
for i in progressbar.progressbar(range(10), suffix='{seconds_elapsed:.1}'):
Expand Down

0 comments on commit 7dff419

Please sign in to comment.