Whether you’re creating a simple Python script or an enterprise-grade application, the elegant console interaction will save you a nasty troubleshooting headache down the road.
In this article, we’ll look at some libraries that will allow you to create nice, elegant console interactions and output for your code.
Using these great libraries will help you create command line applications that your users will love.
The first module is probably one of the most convenient features you can add to your code. Progress indicator! Isn’t it frustrating when you have no idea how much time has passed during a long process?
If you’ve ever installed a package with pip, you’ve seen this module in action.
import tqdm import time for _ in tqdm.tqdm(range(100)): time.sleep(0.25)
Who doesn’t love colorful console text? Errors are displayed in red, successful operations in green.
Using colorama, you can colorize any text output and highlight specific words, phrases, or lines.
from colorama import init init() from colorama import Fore, Back, Style print(Fore.GREEN + 'green text') print(Back.YELLOW + 'yellow back') print(Style.BRIGHT + 'bright' + Style.RESET_ALL) print('default')
About art, I think there is no need to tell, just look at it
from art import tprint tprint("Python")
Sometimes all you need to simplify a complex application is a menu. Love them or hate them, command line menus can make a user’s life easier.
from simple_term_menu import TerminalMenu menu = TerminalMenu(['yes', 'no', 'maybe', 'so']) menu.show()
Printing tables manually with Python is frustrating at best. Headings don’t display correctly, columns shift to the left or right… You can make your life easier with tabulate.
import tabulate data = [ ['id', 'name', 'number'], [0, 'Jeff', 1234], [1, 'Bob', 5678], [2, 'Bill', 9123] ] results = tabulate.tabulate(data) print(results)