Skip to content
Advertisement

Some numbers are automatically formatted using rich.console, how to prevent this?

The following code

from rich.console import Console

console = Console()

console.print("ciao-16S-123")

will print the number 123 highlighted (in blue, in my terminal). This happens on many strings with numbers, what could be the problem that causes this unwanted formatting, and how to prevent it?

enter image description here

Advertisement

Answer

As per Rich documentation, “Rich can apply styles to patterns in text which you print() or log(). With the default settings, Rich will highlight things such as numbers, strings, collections, booleans, None, and a few more exotic patterns such as file paths, URLs and UUIDs.”

You can disable it like this:

console.print("ciao-16S-123", highlight=False)

You can also define a custom highlighter better suited for your needs.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement