Live Table with Progress Bar #482
-
Hello there! Second I was wondering if it is possible to have a table on top of a progress object. On top of this I need then to be controlled by (semi)independent classes.
But the I guess the other I also tried merging the table and progress in one column
but the result is that now nothing works. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Glad you like Rich. Live and Progress probably wouldn't work in combination, but I think you can achieve what you want by extending the Progress class. There is a def get_renderables(self) -> Iterable[RenderableType]:
"""Get a number of renderables for the progress display."""
table = self.make_tasks_table(self.tasks)
yield table If you extend that, you can yield a Columns object with your new table and the table returned from |
Beta Was this translation helpful? Give feedback.
-
For anyone interested here is a working implementation. |
Beta Was this translation helpful? Give feedback.
-
How can I live update a table? Should I clear the table in a loop? |
Beta Was this translation helpful? Give feedback.
-
I've created a standalone class, ChecklistTracker, when I gave up trying to make things line up nicely and juggle trying to have both things work at the same time. https://gist.github.com/kfsone/15b37b28eb93a6aa74a750530a10313f |
Beta Was this translation helpful? Give feedback.
-
A minimal example of such live table: import time
from collections import deque
from rich.console import ConsoleRenderable, Group, RichCast
from rich.progress import Progress
from rich.table import Table
class CustomProgress(Progress):
def __init__(self, table_max_rows: int, *args, **kwargs) -> None:
self.results = deque(maxlen=table_max_rows)
self.update_table()
super().__init__(*args, **kwargs)
def update_table(self, result: tuple[str] | None = None):
if result is not None:
self.results.append(result)
table = Table()
table.add_column("Row ID")
table.add_column("Result", width=20)
for row_cells in self.results:
table.add_row(*row_cells)
self.table = table
def get_renderable(self) -> ConsoleRenderable | RichCast | str:
renderable = Group(self.table, *self.get_renderables())
return renderable
if __name__ == "__main__":
with CustomProgress(table_max_rows=10) as progress:
task = progress.add_task("Task", total=100)
for row in range(100):
time.sleep(0.1)
progress.update(task, advance=1)
progress.update_table((f"{row}", f"Result for row {row}")) |
Beta Was this translation helpful? Give feedback.
Glad you like Rich.
Live and Progress probably wouldn't work in combination, but I think you can achieve what you want by extending the Progress class. There is a
get_renderables
method on Progress which is currently the following:If you extend that, you can yield a Columns object with your new table and the table returned from
makes_tasks_table
.