Skip to content

[REQUEST] Support for deleting/removing rows/columns from table #1331

@cbrxyz

Description

@cbrxyz

Hi! I'm trying to build a UI through the Table class, but I couldn't find a way to remove rows or columns from the table, only add. How I'm getting around it now is by just constantly re-building the table, aka:

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

console = Console()

rows = []

while True:
    user_input = input("Would you like to add or delete?")
    if user_input == "add":
        rows.append("Cell")
    else:
        if len(rows) > 0:
            rows = rows[:-1]
    table = Table("Cells")
    for row in rows:
        table.add_row(row)
    console.print(table)

This works fine, but I wanted to use the Live class, but to do so, I believe there would have to be a remove_row method for the table. I could use the update() method, but I believe I would still have to store the rows in an external array (outside that of the rows array inside the Table class). I would hope remove_row would help to reduce this need.

How would you improve Rich?

I would hope that the remove methods for tables could help to add more flexibility to their generation. Two methods of implementation that I had in mind include:

import time

from rich.table import Table
from rich.live import Live

table = Table("Cells")
with Live(table, refresh_per_second=4):
    time.sleep(0.4)
    user_input = input("Would you like to add or delete?")
    if user_input == "add":
        table.add_row("Cell")
    else:
        table.remove_row() # by default removes the last row, but if index is passed in, removes that row
import time

from rich.table import table
from rich.live import live

table = table("cells")
with live(table, refresh_per_second=4):
    time.sleep(0.4)
    user_input = input("would you like to add or delete?")
    if user_input == "add":
        table.add_row("cell")
    else:
        last_row = table.rows[-1] # gets last row of table
        last_row.remove() # now removes it

What problem does it solve for you?

I believe implementing this function would allow me to remove the need to manage my table data in an external array that I know I can add to and delete from. This would allow me to keep the data inside the Table class and only use the class' methods to manipulate data.

Did I help

If I was able to resolve your problem, consider sponsoring my work on Rich, or buy me a coffee to say thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions