In this blog I am gonna talk about a python module that can make your console based applications look much better than they normally look, the colorama module.
Here's how a normal console application looks without using colorama.
And here's how the same script would look with some color effects.
Isn't that beautiful !!!
All of this was possible due to colorama module. And we are gonna learn how we can use this module in python.
Colorama is a python module that is used to display colored output in console. It can change both, foreground and background color of any text which is displayed in the console.
The link to its github repository is this:
First you need to download and install colorama. Just open your command prompt and type the following:
pip install colorama
sudo pip3 install colorama
This will automatically download and install colorama in your system.
(Note: you should have pip installed in your system for this to work, If it is not, check out this blog and setup your pip)
Once your colorama is installed, you can import colorama in any of your python script and use it. Now let's learn how to use it within our scripts.
First we have to initialize colorama by running the init method, like this:
from colorama import init init()
On Windows, calling
init() will filter ANSI escape sequences out of any text sent to
stderr, and replace them with equivalent Win32 calls.
On other platforms, it's not of much use. So don't forget to call this function is your script runs on Windows.
It's time we write our first colored output on the screen, like this:
from colorama import init, Fore, Back, Style init() print(" Normal white color") print(Fore.RED + " Letters will be in red ") print(Back.GREEN + " Background will be green " + Style.RESET\_ALL) print(Style.DIM + Fore.RED + Back.GREEN + " And style will be dim " + Style.RESET\_ALL) print(" And everything is back to normal")
The output would look like this:
As you might understand, you can change the foreground letter color using the Fore class and selecting the correct color constant you want to use. You can change background color using Back class and change style of letters using Style class.
Here are the possible foreground, background and style class.
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Style: DIM, NORMAL, BRIGHT, RESET_ALL
Note: The color effects that you will place will remain consistent, unless you call deinit() function or mention Fore.RESET, Back.RESET or Style.RESET_ALL.
You can also use termcolor (in Windows) to color a specific portion of the console output
from colorama import init from termcolor import colored init() print(colored("This is in red color", "red")) print(colored("This is in yellow color", "yellow")) print(colored("This is in blue color", "blue")) print(colored("This is in cyan color", "cyan")) print(colored("This is in green color", "green")) print(colored("This is in magenta color", "magenta"))
just import colored function from termcolor and use it as following:
print( colored( "string to color", "color name" ) )
Overall this is a very awesome module and I bet you should definitely try using this module within your scripts, to give it a more professional look and feel.
Hope this blog helped you all :)