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+.
💬 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,
,
,
,
or
, to name a few.
The data to render. Either a list of lists (one per row) or a dict for two-column tables.
header
iterable
Optional header columns.
None
footer
iterable
Optional footer columns.
None
divider
bool
Show a divider line between header/footer and body.
False
widths
iterable / "auto"
Column widths in order. If "auto", widths will be calculated automatically based on the largest value.
"auto"
max_col
int
Maximum column width.
30
spacing
int
Number of spaces between columns.
3
aligns
iterable / unicode
Columns alignments in order. "l" (left, default), "r" (right) or "c" (center). If If a string, value is used for all columns.
None
multiline
bool
If a cell value is a list of a tuple, render it on multiple lines, with one value per line.
False
env_prefix
unicode
Prefix for environment variables, e.g. WASABI_LOG_FRIENDLY.
"WASABI"
color_values
dict
Add or overwrite color values, name mapped to value.
None
fg_colors
iterable
Foreground colors, one per column. None can be specified for individual columns to retain the default background color.
None
bg_colors
iterable
Background colors, one per column. None can be specified for individual columns to retain the default background color.
None
RETURNS
str
The formatted table.
functionrow
from wasabi import row
data = ("a1", "a2", "a3")
formatted = row(data)
a1 a2 a3
Argument
Type
Description
Default
data
iterable
The individual columns to format.
widths
list / int / "auto"
Column widths, either one integer for all columns or an iterable of values. If "auto", widths will be calculated automatically based on the largest value.
"auto"
spacing
int
Number of spaces between columns.
3
aligns
list
Columns alignments in order. "l" (left), "r" (right) or "c" (center).
None
env_prefix
unicode
Prefix for environment variables, e.g. WASABI_LOG_FRIENDLY.
"WASABI"
fg_colors
list
Foreground colors for the columns, in order. None can be specified for individual columns to retain the default foreground color.
None
bg_colors
list
Background colors for the columns, in order. None can be specified for individual columns to retain the default background color.
None
RETURNS
str
The formatted row.
classTracebackPrinter
Helper to output custom formatted tracebacks and error messages. Currently used
in Thinc.
methodTracebackPrinter.__init__
Initialize a traceback printer.
from wasabi import TracebackPrinter
tb = TracebackPrinter(tb_base="thinc", tb_exclude=("check.py",))
Argument
Type
Description
Default
color_error
str / int
Color name or code for errors (passed to color helper).
"red"
color_tb
str / int
Color name or code for traceback headline (passed to color helper).
"blue"
color_highlight
str / int
Color name or code for highlighted text (passed to color helper).
"yellow"
indent
int
Number of spaces to use for indentation.
2
tb_base
str
Name of directory to use to show relative paths. For example, "thinc" will look for the last occurence of "/thinc/" in a path and only show path to the right of it.
Some error
Some error description
Traceback:
├─ <lambda> [61] in .env/lib/python3.6/site-packages/pluggy/manager.py
├─── _multicall [187] in .env/lib/python3.6/site-packages/pluggy/callers.py
└───── pytest_fixture_setup [969] in .env/lib/python3.6/site-packages/_pytest/fixtures.py
>>> result = call_fixture_func(fixturefunc, request, kwargs)
Argument
Type
Description
Default
title
str
The message title.
*texts
str
Optional texts to print (one per line).
highlight
str
Optional sequence to highlight in the traceback, e.g. the bad value that caused the error.
False
tb
iterable
The traceback, e.g. generated by traceback.extract_stack().
None
RETURNS
str
The formatted traceback. Can be printed or raised by custom exception.
classMarkdownRenderer
Helper to create Markdown-formatted content. Will store the blocks added to the
Markdown document in order.
from wasabi import MarkdownRenderer
md = MarkdownRenderer()
md.add(md.title(1, "Hello world"))
md.add("This is a paragraph")
print(md.text)
methodMarkdownRenderer.__init__
Initialize a Markdown renderer.
from wasabi import MarkdownRenderer
md = MarkdownRenderer()
Argument
Type
Description
Default
no_emoji
bool
Don't include emoji in titles.
False
RETURNS
MarkdownRenderer
The renderer.
methodMarkdownRenderer.add
Add a block to the Markdown document.
from wasabi import MarkdownRenderer
md = MarkdownRenderer()
md.add("This is a paragraph")
Argument
Type
Description
Default
text
str
The content to add.
propertyMarkdownRenderer.text
The rendered Markdown document.
md = MarkdownRenderer()
md.add("This is a paragraph")
print(md.text)
from wasabi import color
formatted = color("This is a text", fg="white", bg="green", bold=True)
Argument
Type
Description
Default
text
str
The text to be formatted.
-
fg
str / int
Foreground color. String name or 0 - 256.
None
bg
str / int
Background color. String name or 0 - 256.
None
bold
bool
Format the text in bold.
False
underline
bool
Format the text by underlining.
False
RETURNS
str
The formatted string.
functionwrap
from wasabi import wrap
wrapped = wrap("Hello world, this is a text.", indent=2)
Argument
Type
Description
Default
text
str
The text to wrap.
-
wrap_max
int
Maximum line width, including indentation.
80
indent
int
Number of spaces used for indentation.
4
RETURNS
str
The wrapped text with line breaks.
functiondiff_strings
from wasabi import diff_strings
diff = diff_strings("hello world!", "helloo world")
Argument
Type
Description
Default
a
str
The first string to diff.
b
str
The second string to diff.
fg
str / int
Foreground color. String name or 0 - 256.
"black"
bg
tuple
Background colors as (insert, delete) tuple of string name or 0 - 256.
("green", "red")
RETURNS
str
The formatted diff.
Environment variables
Wasabi also respects the following environment variables. The prefix can be
customised on the Printer via the env_prefix argument. For example, setting
env_prefix="SPACY" will expect the environment variable SPACY_LOG_FRIENDLY.
Name
Description
ANSI_COLORS_DISABLED
Disable colors.
WASABI_LOG_FRIENDLY
Make output nicer for logs (no colors, no animations).
WASABI_NO_PRETTY
Disable pretty printing, e.g. colors and icons.
🔔 Run tests
Fork or clone the repo, make sure you have pytest installed and then run it on
the package directory. The tests are located in
/wasabi/tests.
pip install pytest
cd wasabi
python -m pytest wasabi
Alternatives
Use instead of wasabi: tools that do the same job, closest first.