Randomly Generated Table with Rich Library

  • Share this:

Code introduction


This function uses the Rich library's Table class to generate a table with random columns and data, and uses the Console class for display.


Technology Stack : Rich, Table, Console, random

Code Type : The type of code

Code Difficulty : Intermediate


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

    console = Console()
    table = Table(show_header=True, header_style="bold magenta")

    # Adding random columns
    columns = ["Name", "Age", "City"]
    table.add_column(choice(columns), justify="left")
    table.add_column(choice(columns), justify="left")
    table.add_column(choice(columns), justify="left")

    # Adding random data
    for _ in range(arg1):
        table.add_row([f"Name {_}", f"Age {_}", f"City {_}"])

    # Display the table
    console.print(table)