- Something nice
- Give me some packets
- With code it is better
- Go building
I am coding in Python very often for producing scripts quickly or for bigger projects. In the latter case, I like to have something nice to display. I attach a great importance to the messages I print for debugging, information or help. But sometimes the outputs I have are very long in a terminal and then it becomes interesting to color them for clarity. I will give here the procedure I use to add some colors in my terminal with Python. This is maybe not the best way and definitely not the only way. You can find other tutorials about this.
Give me some packets
I am using two packets; colorama and termcolor. The first one gives the possibility to easily use colors on a terminal for Linux, Mac and Windows. Indeed managing the ANSI escape sequences with all the different platforms can be a painful task. The last package gives some functions to quickly manage text colors, highlights and attributes.
The installation of these packages is straightforward and the same for both of them.
- Download the
- Extract the archive:
tar xzvf <archive>.tar.gz
- Go to the extracted directory:
- Install the package:
sudo python setup.py install
With code it is better
Now comes the coding part. Before giving any examples, it is necessary to include the packages inside the scripts where we want to color the outputs.
The easy way
The easiest method to use colors is to play only with colorama. It suffices to define the foreground, the background, the style, the text to print and reset everything to go back to the default printing of the terminal.
It is possible to output the same result with termcolor lazily by using the
cprint() function. In my opinion, it is a better way because it is more intuitive but the understanding of the ANSI escape sequences is more implicit.
Let is go a bit further by adding an argument and a file descriptor. It can be a powerful tool to log errors or to specify exactly where we want to print the outputs.
Colored might be cool
Still playing with termcolor, I prefer to use the
colored() function because there is the possibility to store the text formatted for being colored in a variable. For instance you can try:
It will print
Hello world! on a blue background and we could be able to reuse the variable
mytext again which can be very handy. Of course you can use the
colored() function like this:
A simple printer
So now that we have seen the main possibilities offered by the packages, it is time to create our own functions. It can also be useful for bigger projects to create Classes and with specific Objects to print things. In this post, I will just show a simple function that can give you some ways to go further.
So let is say I want to print a text and have the possibility to select or not if the output will be printed in bold. I will create a function that can handle the colors and the bold attribute as shown below:
Simple, do not you think? So what is happening here? Basically, the function requires two parameters; the text to print and the color to use. The first part of the function is just creating shortcuts for the colors. In other words, instead of typing
magenta it suffices to type
m. It is a personal choice to write faster. For instance, if I want to print the string
"Hello world!" in yellow, I will simply type:
The second part of the function is related to the
**arg_attrs which is an optional parameter. With the
att_bold variable, we define that we call the bold attribute if we type
b='yes' when using the
myprinter() function. If we continue with our previous string
"Hello world!" and we want to print it using bold this time, we just need to type:
I gave here only one example and a very simple one. Indeed only the bold attribute was handled. It is obviously possible to create different functions for other purposes. For instance in my case, I like to create a
Printer class and I usually define some skeletons for my outputs; one for an information, another one for errors, another one for alerts, and so on. You can find a real example of this in the source code of my project ReporTeX.
Things can become very interesting if you decide to combine colored outputs with logging. Also have a look on the documentations of colorama and termcolor; they are very well written and present other utilization cases that might be useful for your own projects.