Reset a Table with Rich in Python, or delete a row?

2.7k views Asked by At

How could I erase a row table in rich? (Erase the rows or reset all the table)

ex. How could I reset tableTest or erase a row?

from rich.console import Console
from rich.table import Table

tableTest = Table(title="Star Wars Movies")

tableTest.add_column("Released", justify="right", style="cyan", no_wrap=True)
tableTest.add_column("Title", style="magenta")
tableTest.add_column("Box Office", justify="right", style="green")

tableTest.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
tableTest.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
tableTest.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
tableTest.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")

console = Console()
console.print(tableTest)
2

There are 2 answers

0
user459872 On BEST ANSWER

No, rich does not provide any APIs to Delete/Remove the rows. See this Support for deleting/removing rows/columns from table to understand why no such APIs are added to the Table class.

0
Ibrahim Ali On

Just reinitialize the table.

def resetTable():
    global tableTest

    tableTest = Table(title="Star Wars Movies")

    tableTest.add_column("Released", justify="right", style="cyan", no_wrap=True)
    tableTest.add_column("Title", style="magenta")
    tableTest.add_column("Box Office", justify="right", style="green")

And call resetTable() whenever you want to reset your table