Search code examples
pythonpython-3.xprintingrich

Set different background color using Rich


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 :(


Solution

  • 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.