Dennis O'Keeffe
2 min readSep 2, 2021
Heading image

This is Day 13 of the #100DaysOfPython challenge.

This post will use the rich library to demonstrate how to add some color and formatting to your Python CLI output.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. Read Pretty errors in Python for my post on how to make pretty error output in Python.
  3. Read CLI Prompts in Python for my post on how to make CLI prompts in Python.
  4. Read Building CLIs with python-fire for my intro on building CLIs with python-fire that will be used today.
  5. Familiarity with how Python Fire works.

This works from the content of a previous post on Pretty errors in Python.

While not necessary to understand the post, it is recommended that you read it if you are hoping to use the same code.

Getting started

At this stage of the series, our code in cli.py for the project folder should look like the following:

In today’s post, we are hoping to just to show a demo of how easy it is to use rich to add some color to our output.

We can add the rich library to our current Pipenv environment by running the following command:

We are now ready to update our input.

Adding color to our output

We will demo how rich works with simple example of adding color to the DigestionStage class method status.

Currently, the method looks like so:

Currently, if we run our the following commands in the terminal, we get output without color:

What we would like to do is to add some color to the output but updating the function to use the print function from the rich library and update what we return at the end.

We can do this with the following:

Re-running the above commands will adjust the output slightly, but now self.satiated output printed in bold green when true and self.satiated output is printed in red when false.

Final output

Summary

Today’s post demonstrated how to use the rich package to add some color to our output.

Resources and further reading

  1. The ABCs of Pipenv
  2. Pipenv
  3. Pretty errors in Python
  4. CLI Prompts in Python
  5. Building CLIs with python-fire
  6. Python Fire
  7. rich

Photo credit: pawel_czerwinski

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet