wasabi
A lightweight console printing and formatting toolkit
Description
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 with Python 3.6+.
<img width="609" src="https://user-images.githubusercontent.com/13643239/48663861-8c9ea000-ea96-11e8-8b04-d120c52276a8.png">💬 FAQ
Are you going to add more features?
Yes, there's still a few of helpers and features to port over. 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
<kbd>function</kbd> msg
An instance of Printer, initialized with the default config. Useful as a quick
shortcut if you don't need to customize initialization.
from wasabi import msg
msg.good("Success!")
<kbd>class</kbd> Printer
<kbd>method</kbd> 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 | str | Steps of loading animation for Printer.loading. | "⠙⠹⠸⠼⠴⠦⠧⠇⠏" |
animation_ascii | str | Alternative animation for ASCII terminals. | "|/-\\" |
hide_animation | bool | Don't display animation, e.g. for logs. | False |
ignore_warnings | bool | Don't output messages of type MESSAGE.WARN. | False |
env_prefix | str | Prefix for environment variables, e.g. WASABI_LOG_FRIENDLY. | "WASABI" |
timestamp | bool | Add timestamp before output. | False |
| RETURNS | Printer | The initialized printer. | - |
<kbd>method</kbd> Printer.text
msg = Printer()
msg.text("Hello world!")
| Argument | Type | Description | Default |
|---|---|---|---|
title | str | The main text to print. | "" |
text | str | Optional additional text to print. | "" |
color | unicode / int | Color name or value. | None |
icon | str | 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 |
spaced | bool | Whether to add newlines around the output. | False |
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 |
<kbd>method</kbd> 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 | str | The main text to print. | "" |
text | str | 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 |
<kbd>method</kbd> Printer.divider
Print a formatted divider.
msg = Printer()
msg.divider("Heading")
| Argument | Type | Description | Default |
|---|---|---|---|
text | str | Headline text. If empty, only the line is printed. | "" |
char | str | 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 |
icon | str | Optional icon to use with title. | None |
<kbd>contextmanager</kbd> 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 | str | The text to display while loading. | "Loading..." |
<kbd>method</kbd> Printer.table, Printer.row
See Tables.
<kbd>property</kbd> 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. |
Tables
<kbd>function</kbd> table
Lightweight helper to format tabular data.
from wasabi import table
data = [("a1", "a2", "a3"), ("b1", "b2", "b3")]
header = ("Column 1", "Column 2", "Column 3")
widths = (8, 9, 10)
aligns = ("r", "c", "l")
formatted = table(data, header=header, divider=True, widths=w