/ tuesdaytooling

Tuesday Tooling: That's Rich!

The Python REPL is useful, but now we can make it beautiful.

So What s it?

Rich is a Python library for writing rich text (with color and style) to the terminal, and for displaying advanced content such as tables, markdown, and syntax highlighted code. - Rich Website

Created by Will McGugan Rich is a simple Python module which we can use to add a little simple beauty to our scripts.

So How Do I Install It?

Using pip of course!

sudo pip3 install rich


pip3.exe install rich

So What Can I Do With It?

Create colourful output

Custom Prompts To Capture User Input

from rich.text import Text
from rich.console import Console
from rich.prompt import Prompt
console = Console()
name = Prompt.ask("Enter your name", choices=["Les", "Dexter", "Paula"], default="Les")
if name == "Les":
    text = Text("@biglesp woz ere")
    text.stylize("bold red", 0, 8)
    text.stylize("bold green", 8, 12)
    text.stylize("bold blue", 12,16)
    text = Text("Who are you?!")
    text.stylize("bold red", 0, 13)

Create a table

Create a panel to highlight information

from rich import print
from rich.panel import Panel
print(Panel("[red]RED ALERT!", title="ALERT STATUS"))

And there are lots more things that you can do!

Make Something Cool!

Using an old blog post on psutil, I made a quick status app that provides me with the current CPU utilisation, available RAM and my IP address.

First I imported psutil and then the Console and Table classes from the rich module.

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

I then create a link to use the Console() function.

console = Console()

I create a variable to hold the current CPU usage.

cpu = psutil.cpu_percent(interval=1)

I then create a variable memory which initially contains all of the data dumped from the command. I then pull the data that I want, and convert it to Megabytes.

memory = psutil.virtual_memory()
memory = memory[1]
memory = round(memory / 1024 / 1024)

To get my IP address, I need to know my Interface name, and then pull the data for that interface.

interfaces = psutil.net_if_addrs()
ip = interfaces["wlp0s20f3"][0][1]

On to the rich specific bit now. To create a table, we need to give it a title.

table = Table(title="System Status")

Now I add two columns, the first is called Resource and the second is Value. They contain the name of the resource and the value it reports. I also add a little colour to the table.

table.add_column("Resource", justify="right", style="cyan", no_wrap=True)
table.add_column("Value", style="magenta")

The three rows of the table are my CPU, RAM and IP address.

table.add_row("CPU", str(cpu)+"%")
table.add_row("RAM", str(memory)+"MB Free")
table.add_row("IP Address", str(ip))

I then display the table to the REPL.


Save and run the code to see your own table full of the data that you need.
I saved my project as an executable file and launch it whenever I start a new BASH terminal.

Happy Hacking