Using Rich Library for CLI Applications
CLI applications are pretty unsexy. Let’s face it, digging around in the CLI is hard for a lot of people, and even though you can make some cool stuff in there, you definitely want to find a way to spice up your applications so that you don’t have to worry about people getting bored with your program. Today I’m going to be going through a couple of interesting features with a library called “Rich”. Rich is pretty lightweight and easy to use, and a great way of adding some simple formatting to any CLI application.
First things first, you have to actually get rich into your application. This is actually pretty straightforward, just drop : <python -m pip install rich> in your terminal and it will install the program (assuming pip or a compatible package manager). To make sure that the install went through simply type <python -m rich> and you will see a brief demo of some of the capabilities that rich has:
Some of the main features are highlighted on the left side of the terminal above, specifically around colors and styles, but it also has other cool features like tables, progress bars, columns, and boxes, as well as supporting general “Markdown” messages. We will be covering a few of the basics in this blog.
Before we can do anything though, in the top of your python file you will need to add : <from rich.console import Console>. The reason for this is when you are printing something out or creating an element with Rich, it often needs a way of routing through rich, rather than a standard method of presentation. So for example, to print something with rich you will use Console.print() rather than just print(). We will see it applied in a few examples below, so if it doesn’t make sense right now, don’t worry!
Colors and formatting:
Okay, so really simple, let’s say that I have a line of text that I want to present. Well, we can use Rich styles to really take control of the format of what we are presenting. If you are really interested in learning more, here is a link to the documentation. Below are a few basics that might be enough to get you started, however.
Okay, so lets say that I want to print a message out that says something like “Oh hello there you silly little stinker” (although I don’t know what program that would be for), as we all know in python it would normally look like this:
print(“Oh hello there you silly little stinker”) and it would output:
Great, but what if I want to make this a little more interesting? Well, there are style tags that you can use when you import rich, here is a link to the complete list, all you have to do is import Console from rich. console, set console= Console(), then instead of a normal print statement, do “console.print” and after the quotes to denote the print content, add style=”insert your style tags in here”, so that it looks like this:
Neato, now all we have to do is select the style tags that we want and drop them in, and Rich does the rest. There are simple ones like bold or underline, or strikethrough, but Rich also has a couple like “blink” which will make the text blink or support colors as well, so you can make the text whatever color you want. It can even modify the text based on commands more complicated like “White on Black” which would make it white text on a black background. So if I REALLY wanted to drive home that someone was a silly little stinker it might look like this:
Play around with it yourself though!
Tables:
Another easy application is tables. All that you need to do is make sure that you import tables from rich like this: <from rich.table import Table>, then you are all set up to start making some beautiful looking tables in your CLI. Normally it’s difficult to format data how you want to in a CLI application, but the tables element in Rich makes things look a lot cleaner.
Step one, is you have to import the tables from rich with <from rich.table import Table>, then in your python file you will be able to create three things for a beautiful table — the table itself, columns, and finally rows.
To set up a table simply add <table = Table(title=”insert your title here”)> — I would recommend saving it instead of just as “table” something a bit more descriptive in case you need to do more than one. next you add the columns / headers in one at a time. To do this it’s just the table variable name and then .add_column(“header name”), then finally for rows, itps pretty similar, it’s just the table variable name with .add_row(“column 1 content”, “column 2 content”, “etc” ). I’ll show you a sample of what the completed code might look like with some completely random data:
And that outputs this into the command line:
Pretty nifty, eh?
Between the styles and the tables, this should be enough to start you on the path of better looking CLI applications, but I highly recommend checking out the documentation to really grasp the full scope of what Rich can do!