Logo

dev-resources.site

for different kinds of informations.

Making Your CLI Applications Pop with Styled Outputs

Published at
1/10/2025
Categories
cli
python
styling
beginners
Author
kelseyroche
Categories
4 categories in total
cli
open
python
open
styling
open
beginners
open
Author
11 person written this
kelseyroche
open
Making Your CLI Applications Pop with Styled Outputs

Console apps don’t have to be boring! Sure, they’re often thought of as plain and functional, but adding a bit of color, bold text, and style can make a world of difference. With Python libraries like colorama and rich, it’s surprisingly easy to give your CLI tools some personality. Let’s check out how you can bring them to life!

Why Style Your CLI Outputs?

  • Improve Usability: Highlight important messages like errors or warnings.

  • Enhance Readability: Use color coding to organize output.

  • Make It Fun: Add personality to your application with creative styling.

colorama

colorama is a lightweight library that enables ANSI color codes on Windows and other platforms, making it a great choice for simple styling needs.

Installation:

pip install colorama

Enter fullscreen mode Exit fullscreen mode

Basic Usage:
Here’s how to use colorama to style your console outputs:

from colorama import Fore, Back, Style, init

Enter fullscreen mode Exit fullscreen mode

Initialize colorama

init()

print(Fore.RED + "This is red text")
print(Back.YELLOW + "This has a yellow background")
print(Style.BRIGHT + "This text is bold")
print(Style.RESET_ALL + "Back to normal style")

Enter fullscreen mode Exit fullscreen mode

Output:

  • Red text
  • Yellow background
  • Bold text
  • Reset to normal

Use Case: Highlight error messages:

print(Fore.RED + "Error: Invalid input!" + Style.RESET_ALL)

Enter fullscreen mode Exit fullscreen mode

rich

If you want more advanced features, like tables, progress bars, and markdown support, rich is an excellent choice.

Installation:

pip install rich

Enter fullscreen mode Exit fullscreen mode

Basic Styling:

from rich.console import Console

console = Console()

console.print("[bold magenta]Hello, World![/bold magenta]")
console.print("[red]Error:[/red] Something went wrong.")
Enter fullscreen mode Exit fullscreen mode

Output:

  • Bold magenta text
  • Red text with “Error” label

Use Case: Formatting a table:

from rich.table import Table

table = Table(title="Sample Table")

table.add_column("Name", style="cyan")
table.add_column("Age", justify="right", style="green")
table.add_row("Alice", "30")
table.add_row("Bob", "25")

console.print(table)
Enter fullscreen mode Exit fullscreen mode

Combining colorama and rich

You can combine the simplicity of colorama for basic styling and the advanced features of rich to create a dynamic CLI experience.

Example:

from colorama import Fore, Style, init
from rich.console import Console
Enter fullscreen mode Exit fullscreen mode

Initialize colorama

init()

Enter fullscreen mode Exit fullscreen mode

Create a rich console

console = Console()

print(Fore.YELLOW + "Welcome to the Styled CLI App!" + Style.RESET_ALL)
console.print("[green]Let's get started![/green]")
Enter fullscreen mode Exit fullscreen mode

Tips for Effective CLI Styling

  • Use Color Consistently: Assign specific colors to warnings, errors, and success messages.
  • Don’t Overdo It: Too much color or bold text can overwhelm users.
  • Test for Readability: Ensure your colors are readable on light and dark terminal themes.
  • Provide a Plain Mode: Offer a way to disable styling for users who prefer plain text.

Conclusion

Styling your CLI outputs can significantly enhance user experience, making your applications more functional and fun. Whether you use colorama for lightweight color support or rich for advanced formatting, Python makes it easy to create visually appealing console tools.

Have you tried adding styles to your CLI apps? Let me know your experiences in the comments!

Resources:
https://pypi.org/project/colorama/
https://www.geeksforgeeks.org/introduction-to-python-colorama/
https://rich.readthedocs.io/en/stable/introduction.html
https://www.geeksforgeeks.org/installing-and-using-rich-package-in-python/

styling Article's
30 articles in total
Favicon
Making Your CLI Applications Pop with Styled Outputs
Favicon
Decoding StyleX: Meta's Cutting-Edge Styling System
Favicon
Free Tailwind CSS Button Animations
Favicon
Styling in React
Favicon
Need Help With Z-Index/Positiong
Favicon
Styling in React: CSS-in-JS vs. Traditional CSS
Favicon
Panda CSS Conditions
Favicon
Styling Your Next.js Web Application
Favicon
DIV Class and CSS. A beginner explains.
Favicon
Personalizing MUI Stepper
Favicon
CSS's :root Variables. Use it!
Favicon
2 Style Patterns in React for Extended Component Styling
Favicon
Complete beginner's guide to using Styled Components with Nextjs and TypeScript
Favicon
Here, not there — or, making styling behave
Favicon
WHY TAILWIND CSS WHEN I HAVE BOOTSTRAP
Favicon
Darle estilo a un input file upload button <input type="file">
Favicon
Material UI vs Semantic UI vs Styled Components
Favicon
7 Tailwind CSS Tips I Wish I Knew Earlier
Favicon
Boost your styling with Bootstrap 🚀
Favicon
React Styling Methods
Favicon
Module scoped styling in Angular 13
Favicon
How to Detect User Color Preference in React Native
Favicon
CSS Styling Tricks: Flexbox
Favicon
CSS Styling Tricks: How to Build Responsive Web Applications with Percentages
Favicon
Here is why I switched from bootstrap to tailwind.
Favicon
Tailwind vs Sass/SCSS: Structure and Consistency over Style and Comfort
Favicon
What is calc() function in CSS ?
Favicon
Accent all areas
Favicon
CSS basic 3 - Units, Text Styling
Favicon
Pourquoi TailwindCSS est le meilleur moyen de styliser une page web ?

Featured ones: