Set different background color using Rich

Question:

I found useful package rich. And want to use it in the next way:
drawing in different colors words in sentence and at different background colors.
I use next code:

from rich.console import Console
console = Console()
console.print('[green]some[/green] [#F47983]text[/#F47983]')

This way I can set any color for any word:

enter image description here

But I don’t know, how to set different background colors. I tried background= , bg: and backgrounp-color – this doesn’t work 🙁

Asked By: Mikhail_Sam

||

Answers:

Use the word "on" to introduce the background color. For example:

print("[bold green on blue]Fear is the mind-killer[/]")

Also if you want to use hex codes:

print('[green on #F47983]some[/] [#F47983]text[/]')

See https://rich.readthedocs.io/en/latest/style.html for how to define styles.

Answered By: Will McGugan
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.