Rich Library Table Generation with Custom Data

  • Share this:

Code introduction


This function uses the Rich library to create a simple table and prints it out. It takes two arguments that are used to generate the age and occupation data in the table.


Technology Stack : Rich library, Table class

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def generate_random_table(arg1, arg2):
    from rich.console import Console
    from rich.table import Table
    console = Console()

    table = Table(show_header=True, header_style="bold magenta")
    table.add_column("Name", justify="left")
    table.add_column("Age", justify="right")
    table.add_column("Occupation", justify="left")

    for i in range(5):
        table.add_row(f"Person {i+1}", str(arg1 + i), f"Occupation {arg2 + i}")

    console.print(table)