wasabi

🍣 A lightweight console printing and formatting toolkit


Keywords
console, console-color, formatting, python, python-2, python-3, spacy
License
MIT
Install
conda install -c anaconda wasabi

Documentation

wasabi: A lightweight console printing and formatting toolkit

Over the years, I've written countless implementations of coloring and formatting utilities to output messages in our libraries like spaCy, Thinc and Prodigy. While there are many other great open-source options, I've always ended up wanting something slightly different or slightly custom.

This package is still a work in progress and aims to bundle those utilities in a standardised way so they can be shared across our other projects. It's super lightweight, has zero dependencies and works across Python 2 and 3.

Travis PyPi GitHub Code style: black

💬 FAQ

Are you going to add more features?

Yes, there's still a bunch of helpers and features to port over – for example, Thinc's custom traceback printer. I'd also love to add a nice training results table utility, since this is something that we use a lot.

However, the new features will be heavily biased by what we (think we) need. I always appreciate pull requests to improve the existing functionality – but I want to keep this library as simple, lightweight and specific as possible.

Can I use this for my projects?

Sure, if you like it, feel free to adopt it! Just keep in mind that the package is very specific and not intended to be a full-featured and fully customisable formatting library. If that's what you're looking for, you might want to try other packages – for example, colored, crayons, colorful, tabulate, console or py-term, to name a few.

Why wasabi?

I was looking for a short and descriptive name, but everything was already taken. So I ended up naming this package after one of my rats, Wasabi. 🐀

⌛️ Installation

pip install wasabi

🎛 API

class Printer

method Printer.__init__

from wasabi import Printer

msg = Printer()
Argument Type Description Default
pretty bool Pretty-print output with colors and icons. True
no_print bool Don't actually print, just return. False
colors dict Add or overwrite color values, names mapped to 0-256. None
icons dict Add or overwrite icon. Name mapped to unicode. None
line_max int Maximum line length (for divider). 80
animation unicode Steps of loading animation for Printer.loading. "⠙⠹⠸⠼⠴⠦⠧⠇⠏"
animation_ascii unicode Alternative animation for ASCII terminals. "|/-\\"
ignore_warnings bool Don't output messages of type MESSAGE.WARN. False
RETURNS Printer The initialized printer. -

method Printer.text

msg = Printer()
msg.text("Hello world!")
Argument Type Description Default
title unicode The main text to print. ""
text unicode Optional additional text to print. ""
color  unicode / int Color name or value. None
icon unicode Name of icon to add. None
show bool Whether to print or not. Can be used to only output messages under certain condition, e.g. if --verbose flag is set. True
no_print bool Don't actually print, just return. Overwrites global setting. False
exits int If set, perform a system exit with the given code after printing. None

method Printer.good, Printer.fail, Printer.warn, Printer.info

Print special formatted messages.

msg = Printer()
msg.good("Success")
msg.fail("Error")
msg.warn("Warning")
msg.info("Info")
Argument Type Description Default
title unicode The main text to print. ""
text unicode Optional additional text to print. ""
show bool Whether to print or not. Can be used to only output messages under certain condition, e.g. if --verbose flag is set. True
exits int If set, perform a system exit with the given code after printing. None

method Printer.divider

Print a formatted divider.

msg = Printer()
msg.divider("Heading")
Argument Type Description Default
text unicode Headline text. If empty, only the line is printed. ""
char unicode Single line character to repeat. "="
show bool Whether to print or not. Can be used to only output messages under certain condition, e.g. if --verbose flag is set. True

contextmanager Printer.loading

msg = Printer()
with msg.loading("Loading..."):
    # Do something here that takes longer
    time.sleep(10)
msg.good("Successfully loaded something!")
Argument Type Description Default
text unicode The text to display while loading. ""

property Printer.counts

Get the counts of how often the special printers were fired, e.g. MESSAGES.GOOD. Can be used to print an overview like "X warnings"

msg = Printer()
msg.good("Success")
msg.fail("Error")
msg.warn("Error")

print(msg.counts)
# Counter({'good': 1, 'fail': 2, 'warn': 0, 'info': 0})
Argument Type Description
RETURNS Counter The counts for the individual special message types.

Utilities

function color

from wasabi import color

formatted = color("This is a text", fg="white", bg="green", bold=True)
Argument Type Description Default
text unicode The text to be formatted. -
fg unicode / int Foreground color. String name or 0 - 256. None
bg unicode / int Background color. String name or 0 - 256. None
bold bool Format the text in bold. False
RETURNS unicode The formatted string.

function wrap

from wasabi import wrap

wrapped = wrap("Hello world, this is a text.", indent=2)
Argument Type Description Default
text unicode The text to wrap. -
wrap_max int Maximum line width, including indentation. 80
indent int Number of spaces used for indentation. 4
RETURNS unicode The wrapped text with line breaks.

🔔 Run tests

Fork or clone the repo, make sure you have pytest installed and then run it on the directory /tests:

pip install pytest
cd wasabi
python -m pytest tests