Randomly Displayed Data Table with Rich Library

  • Share this:

Code introduction


This function uses the Table class from the Rich library to create and display a simple table. The table content is randomly selected from the data list passed in.


Technology Stack : Rich

Code Type : Rich Table Display

Code Difficulty : Intermediate


                
                    
def display_random_table(data):
    from rich.table import Table
    from random import shuffle

    # Shuffle the data to get a random order
    shuffle(data)

    # Create a new table
    table = Table(show_header=True, header_style="bold magenta")

    # Add headers
    table.add_column("Name", justify="left", style="magenta")
    table.add_column("Age", justify="right", style="magenta")
    table.add_column("City", justify="left", style="magenta")

    # Add data rows
    for item in data:
        table.add_row(item["Name"], str(item["Age"]), item["City"])

    # Print the table
    print(table)                
              
Tags: