Debian Codegraph @

examples/dynamic_progress.py

raw
1 """
2
3 Demonstrates how to create a dynamic group of progress bars,
4 showing multi-level progress for multiple tasks (installing apps in the example),
5 each of which consisting of multiple steps.
6
7 """
8
9 import time
10
11 from rich.console import Group
12 from rich.panel import Panel
13 from rich.live import Live
14 from rich.progress import (
15 BarColumn,
16 Progress,
17 SpinnerColumn,
18 TextColumn,
19 TimeElapsedColumn,
20 )
21
22
23 def run_steps(name, step_times, app_steps_task_id):
24 """Run steps for a single app, and update corresponding progress bars."""
25
26 for idx, step_time in enumerate(step_times):
27 # add progress bar for this step (time elapsed + spinner)
28 action = step_actions[idx]
29 step_task_id = step_progress.add_task("", action=action, name=name)
30
31 # run steps, update progress
32 for _ in range(step_time):
33 time.sleep(0.5)
34 step_progress.update(step_task_id, advance=1)
35
36 # stop and hide progress bar for this step when done
37 step_progress.stop_task(step_task_id)
38 step_progress.update(step_task_id, visible=False)
39
40 # also update progress bar for current app when step is done
41 app_steps_progress.update(app_steps_task_id, advance=1)
42
43
44 # progress bar for current app showing only elapsed time,
45 # which will stay visible when app is installed
46 current_app_progress = Progress(
47 TimeElapsedColumn(),
48 TextColumn("{task.description}"),
49 )
50
51 # progress bars for single app steps (will be hidden when step is done)
52 step_progress = Progress(
53 TextColumn(" "),
54 TimeElapsedColumn(),
55 TextColumn("[bold purple]{task.fields[action]}"),
56 SpinnerColumn("simpleDots"),
57 )
58 # progress bar for current app (progress in steps)
59 app_steps_progress = Progress(
60 TextColumn(
61 "[bold blue]Progress for app {task.fields[name]}: {task.percentage:.0f}%"
62 ),
63 BarColumn(),
64 TextColumn("({task.completed} of {task.total} steps done)"),
65 )
66 # overall progress bar
67 overall_progress = Progress(
68 TimeElapsedColumn(), BarColumn(), TextColumn("{task.description}")
69 )
70 # group of progress bars;
71 # some are always visible, others will disappear when progress is complete
72 progress_group = Group(
73 Panel(Group(current_app_progress, step_progress, app_steps_progress)),
74 overall_progress,
75 )
76
77 # tuple specifies how long each step takes for that app
78 step_actions = ("downloading", "configuring", "building", "installing")
79 apps = [
80 ("one", (2, 1, 4, 2)),
81 ("two", (1, 3, 8, 4)),
82 ("three", (2, 1, 3, 2)),
83 ]
84
85 # create overall progress bar
86 overall_task_id = overall_progress.add_task("", total=len(apps))
87
88 # use own live instance as context manager with group of progress bars,
89 # which allows for running multiple different progress bars in parallel,
90 # and dynamically showing/hiding them
91 with Live(progress_group):
92 for idx, (name, step_times) in enumerate(apps):
93 # update message on overall progress bar
94 top_descr = "[bold #AAAAAA](%d out of %d apps installed)" % (idx, len(apps))
95 overall_progress.update(overall_task_id, description=top_descr)
96
97 # add progress bar for steps of this app, and run the steps
98 current_task_id = current_app_progress.add_task("Installing app %s" % name)
99 app_steps_task_id = app_steps_progress.add_task(
100 "", total=len(step_times), name=name
101 )
102 run_steps(name, step_times, app_steps_task_id)
103
104 # stop and hide steps progress bar for this specific app
105 app_steps_progress.update(app_steps_task_id, visible=False)
106 current_app_progress.stop_task(current_task_id)
107 current_app_progress.update(
108 current_task_id, description="[bold green]App %s installed!" % name
109 )
110
111 # increase overall progress now this task is done
112 overall_progress.update(overall_task_id, advance=1)
113
114 # final update for message on overall progress bar
115 overall_progress.update(
116 overall_task_id, description="[bold green]%s apps installed, done!" % len(apps)
117 )