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

Question:

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)
Asked By: Todeshine

||

Answers:

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.

Answered By: Abdul Niyas P M

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

Answered By: ibrahimxcool
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.